In [35]:
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline

sns.set_context('notebook', font_scale=1.5, rc={'lines.markeredgewidth': 2})
sns.set_style('white', {'axes.spines.right': False, 'axes.spines.top': False, 'xtick.bottom': True, 'ytick.left': True,})
sns.set_palette('deep')
In [36]:
%load_ext autoreload
%autoreload 2
The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
In [37]:
import openscope_predictive_coding as opc
In [38]:
from openscope_predictive_coding.ophys.dataset.openscope_predictive_coding_dataset import OpenScopePredictiveCodingDataset
from openscope_predictive_coding.ophys.response_analysis.response_analysis import ResponseAnalysis
In [39]:
import openscope_predictive_coding.ophys.plotting.experiment_summary_figures as esf
import openscope_predictive_coding.ophys.plotting.summary_figures as sf

import openscope_predictive_coding.ophys.response_analysis.utilities as ut
In [40]:
cache_dir = r'\\allen\programs\braintv\workgroups\nc-ophys\opc\opc_analysis'
In [193]:
manifest_file = os.path.join(cache_dir, 'opc_production_manifest.xlsx')
In [194]:
data = pd.read_excel(manifest_file)
In [ ]:
 
In [195]:
data = data[data['experiment_state']=='passed']
In [196]:
print('total mice:', len(data['mouse_id'].unique()))
print('# V1 injections:', len(data[data['injection_area']=='VISp']['mouse_id'].unique()))
print('# RSP injections:', len(data[data['injection_area']=='RSP']['mouse_id'].unique()))
print('# V1 imaging sessions:', len(data[data['imaging_area']=='VISp']['experiment_id'].unique()))
print('# RSP imaging sessions:', len(data[data['imaging_area']=='RSP']['experiment_id'].unique()))
print('# PM imaging sessions:', len(data[data['imaging_area']=='VISpm']['experiment_id'].unique()))
total mice: 12
# V1 injections: 5
# RSP injections: 7
# V1 imaging sessions: 14
# RSP imaging sessions: 10
# PM imaging sessions: 22
In [197]:
expt_ids = data.experiment_id.unique()
expt_ids = np.sort([int(e) for e in expt_ids if np.isnan(e) == False])
print(expt_ids)
[768898762 775058863 775613721 776727982 813071318 816795279 817251851
 818894752 826576489 827232898 827235482 828956958 828959377 829411383
 829417358 830075254 830688059 830688102 831312165 831314921 832107135
 832601977 832617299 833599179 833611925 833612445 833614835 833626456
 834244626 834251985 834260382 835642229 835654507 835660148 836246273
 836248932 836250018 836253258 836890936 836891984 836906598 837283374
 837285285 837287590 837630919 838330377]

get example expt

In [198]:
dataset = OpenScopePredictiveCodingDataset(expt_ids[1], cache_dir)
analysis = ResponseAnalysis(dataset, preload_response_dfs=False)
In [199]:
sequence_images = analysis.get_sequence_images()
oddball_images = analysis.get_oddball_images()
images = analysis.get_image_ids()
In [200]:
sequence_colors = sns.color_palette("Blues", len(sequence_images))
oddball_colors = sns.color_palette("Reds", len(oddball_images))

load multi session dfs

In [201]:
cache_dir = r'\\allen\programs\braintv\workgroups\nc-ophys\opc\opc_analysis'
In [202]:
oddball_df = pd.read_hdf(os.path.join(cache_dir, 'multi_session_summary_dfs', 'mean_oddball_df_events.h5'))
In [203]:
transitions_df = pd.read_hdf(os.path.join(cache_dir, 'multi_session_summary_dfs', 'mean_transition_control_df_events.h5'))
In [204]:
occlusion_df = pd.read_hdf(os.path.join(cache_dir, 'multi_session_summary_dfs', 'mean_occlusion_df_events.h5'))
In [205]:
pre_df = pd.read_hdf(os.path.join(cache_dir, 'multi_session_summary_dfs', 'mean_randomized_control_pre_df_events.h5'))
# post_df = pd.read_hdf(os.path.join(cache_dir, 'multi_session_summary_dfs', 'mean_randomized_control_post_df_events.h5'))
# nmdf = pd.read_hdf(os.path.join(cache_dir, 'multi_session_summary_dfs', 'mean_natural_movie_one_df.h5'))
In [206]:
odf = oddball_df.copy()
ocdf = occlusion_df.copy()
tdf = transitions_df.copy()
In [207]:
cell_specimen_ids = odf.cell_specimen_id.unique()

merge with injection target

In [ ]:
def get_manifest(cache_dir=None): 
    """
    Loads experiment manifest file as a dataframe, listing all experiments and associated metadata
    """
    if cache_dir is None: 
        cache_dir = r'\\allen\programs\braintv\workgroups\nc-ophys\opc\opc_analysis'
    manifest_file = os.path.join(cache_dir, 'opc_production_manifest.xlsx')
    manifest = pd.read_excel(manifest_file)
    return manifest
In [227]:
def add_projection_pathway_to_df(df, cache_dir=None):
    """
    df: dataframe to add projection pathway information to. Dataframe must have a column 'experiment_id'.
    cache_dir: cache directory to load manifest from 
    
    Adds columns called 'injection_area', the brain area where the retrograde tracer was injected, 
    and 'projection_pathway', indicating whether retrogradely labeled cells are part of a feed forward ('FF') or feed back ('FB') pathway,
    to experiment manifest table, then merges in with provided dataframe. 
    """
    manifest = get_manifest(cache_dir)
    manifest['projection_pathway'] = np.nan
    manifest.at[manifest[manifest.injection_area=='RSP'].index.values, 'projection_pathway'] = 'FF'
    manifest.at[manifest[manifest.injection_area=='VISp'].index.values, 'projection_pathway'] = 'FB'
    manifest.at[manifest[(manifest.imaging_area=='VISpm')&(manifest.injection_area=='RSP')].index.values, 'projection_pathway'] = 'FF'
    manifest.at[manifest[(manifest.imaging_area=='VISpm')&(manifest.injection_area=='VISp')].index.values, 'projection_pathway'] = 'FB'
    df = df.merge(manifest[['experiment_id','injection_area','pathway']], on='experiment_id')
    return data
In [228]:
odf = odf.merge(data[['experiment_id','injection_area','pathway']], on='experiment_id')
ocdf = ocdf.merge(data[['experiment_id','injection_area','pathway']], on='experiment_id')
tdf = tdf.merge(data[['experiment_id','injection_area','pathway']], on='experiment_id')
pre_df = pre_df.merge(data[['experiment_id','injection_area','pathway']], on='experiment_id')

annotate dfs

In [59]:
def add_location_to_df(df):
    """
    Add useful columns, including 'depth' which translates 'imaging_depth' integer values in um to a string indicating superficial or deep layers, 
    and 'location', a string combining the imaged area and the 'depth' string as a way to easily group data by both area and depth for analysis.
    """
    df['area'] = df.targeted_structure.values
    df['depth'] = ['deep' if depth > 250 else 'superficial' for depth in df.imaging_depth.values]
    df['location'] = None
    df['location'] = [df.iloc[row].area+'_'+df.iloc[row].depth for row in range(len(df))]
    return df
In [60]:
# odf = add_location_to_df(odf)
# ocdf = add_location_to_df(ocdf)
# tdf = add_location_to_df(tdf)
In [61]:
# pre_df = add_location_to_df(pre_df)
# post_df = add_location_to_df(post_df)
# nmdf = add_location_to_df(nmdf)
In [62]:
def add_retrogradely_labeled_column_to_df(df):
    """
    takes any dataframe with a column for 'cell_specimen_id' and adds a new column called 'retrogradely_labeled' which is a boolean for whether the cell was tagged or not
    """
    cache_dir = r'\\allen\programs\braintv\workgroups\nc-ophys\opc\opc_analysis'
    cache_dir = r'C:\Users\marinag\Dropbox\opc_analysis'
    red_label_df = pd.read_hdf(os.path.join(cache_dir, 'multi_session_summary_dfs', 'red_label_df.h5'), key='df')
    red_label_df.reset_index(inplace=True)
    red_label_df['cell_specimen_id'] = [int(cell_specimen_id) for cell_specimen_id in red_label_df.cell_specimen_id.values]
    red_df = red_label_df[['cell_specimen_id', 'retrogradely_labeled']]
    df = pd.merge(df, red_df, left_on='cell_specimen_id', right_on='cell_specimen_id')
    return df
In [63]:
# odf = add_retrogradely_labeled_column_to_df(odf)
# ocdf = add_retrogradely_labeled_column_to_df(ocdf)
# tdf = add_retrogradely_labeled_column_to_df(tdf)
In [64]:
# pre_df = add_retrogradely_labeled_column_to_df(pre_df)
# # post_df = add_retrogradely_labeled_column_to_df(post_df)
# nmdf = add_retrogradely_labeled_column_to_df(nmdf)
In [65]:
odf.keys()
Out[65]:
Index(['cell_specimen_id', 'image_id', 'oddball', 'mean_response',
       'sem_response', 'mean_trace', 'sem_trace', 'pref_stim',
       'fraction_significant_trials', 'fraction_responsive_trials',
       'experiment_id', 'experiment_container_id', 'targeted_structure',
       'imaging_depth', 'cre_line', 'reporter_line', 'full_genotype',
       'session_type', 'donor_id', 'experiment_date', 'specimen_id', 'depth',
       'location', 'session_num', 'area', 'retrogradely_labeled'],
      dtype='object')

validate fraction responsive

In [66]:
def plot_mean_trace_with_variability(traces, frame_rate, ylabel='dF/F', label=None, color='k', interval_sec=1,
                                     xlims=[-4, 4], ax=None):
#     xlim = [xlims[0] + np.abs(xlims[0]), xlims[1] + np.abs(xlims[0])]
    if ax is None:
        fig, ax = plt.subplots()
    if len(traces) > 0:
        mean_trace = np.mean(traces, axis=0)
        times = np.arange(0, len(mean_trace), 1)
        sem = (traces.std()) / np.sqrt(float(len(traces)))
        for trace in traces:
            ax.plot(trace, linewidth=1, color='gray')
        ax.plot(mean_trace, label=label, linewidth=3, color=color, zorder=100)
        xticks, xticklabels = sf.get_xticks_xticklabels(mean_trace, frame_rate, interval_sec, window=xlims)
        ax.set_xticks(xticks)
        ax.set_xticklabels([int(x) for x in xticklabels])
        ax.set_xlim(0,(np.abs(xlims[0])+xlims[1]) * int(frame_rate))
        ax.set_xlabel('time (sec)')
        ax.set_ylabel(ylabel)
        sns.despine(ax=ax)
    return ax
In [261]:
dataset = OpenScopePredictiveCodingDataset(expt_ids[1], cache_dir)
analysis = ResponseAnalysis(dataset, regenerate_dfs=True, use_events=True)
In [282]:
tmp = analysis.get_response_df('oddball')
generating df for oddball
generating response dataframe for oddball _events
In [283]:
tmp.keys()
Out[283]:
Index(['stimulus_presentations_id', 'cell_specimen_id', 'trace',
       'trace_timestamps', 'mean_response', 'baseline_response',
       'summed_response', 'p_value', 'index', 'sweep', 'start_frame',
       'end_frame_inclusive', 'start_time', 'end_time', 'session_block_name',
       'image_id', 'repeat', 'fraction_occlusion', 'duration', 'session_type',
       'stimulus_key', 'data_file_index', 'data_file_name', 'frame_list',
       'mean_running_speed', 'oddball', 'sequence_start',
       'violation_sequence'],
      dtype='object')

plot for one

In [79]:
save_dir = r'\\allen\programs\braintv\workgroups\nc-ophys\opc\opc_analysis\summary_figures'
image_ids = sequence_images+oddball_images

experiment_id = expt_ids[0]
print(experiment_id)
dataset = OpenScopePredictiveCodingDataset(experiment_id, cache_dir)
analysis = ResponseAnalysis(dataset, preload_response_dfs=False, use_events=True)
oddball_df = analysis.get_response_df('oddball')
transition_df = analysis.get_response_df('transition_control')
cell_specimen_ids = dataset.get_cell_specimen_ids()
meta = dataset.metadata
768898762
no response dataframe file for oddball - generating
generating response dataframe for oddball _events
loading response dataframe for transition_control

plot mean response

In [95]:
cell_specimen_id = cell_specimen_ids[10] 
figsize = (25,6)
fig, ax = plt.subplots(2, len(image_ids), figsize=figsize, sharey=True)
ax = ax.ravel()
for i,image_id in enumerate(image_ids): 
    df = oddball_df[(oddball_df.cell_specimen_id==cell_specimen_id)&(oddball_df.image_id==image_id)]
    ax[i] = sf.plot_mean_trace(df.trace.values, 31., ylabel='dF/F', legend_label=None, color='k', interval_sec=0.5,
                                         xlims=[-0.5, 0.5], ax=ax[i])
    fraction_sig_trials = odf[(odf.cell_specimen_id==cell_specimen_id)&(odf.image_id==image_id)].fraction_significant_trials.values[0]
    ax[i].set_title(str(int(image_id))+', '+str(np.round(fraction_sig_trials,2)))
    ax[i].set_ylabel('')
    ax[i].axvline(x=2*31, ymin=0, ymax=1, color=sns.color_palette()[0], linestyle='--')

    df = transition_df[(transition_df.cell_specimen_id==cell_specimen_id)&(transition_df.image_id==image_id)&(transition_df.second_in_sequence==True)]
    ax[i+len(image_ids)] = sf.plot_mean_trace(df.trace.values, 31., ylabel='dF/F', legend_label=None, color='k', interval_sec=0.5,
                                         xlims=[-0.5, 0.5], ax=ax[i+len(image_ids)])
    fraction_sig_trials = tdf[(tdf.cell_specimen_id==cell_specimen_id)&(tdf.image_id==image_id)].fraction_significant_trials.values[0]
    ax[i+len(image_ids)].set_title(str(int(image_id))+', '+str(np.round(fraction_sig_trials,2)))
    ax[i+len(image_ids)].set_ylabel('')
    ax[i+len(image_ids)].axvline(x=2*31, ymin=0, ymax=1, color=sns.color_palette()[0], linestyle='--')  

ax[0].set_ylabel('dF/F')
ax[len(image_ids)].set_ylabel('dF/F')
fig.tight_layout()
plt.gcf().subplots_adjust(top=0.85)
title = 'cell_specimen_id: '+str(cell_specimen_id)+', experiment_id: '+str(meta.ophys_experiment_id.values[0])+', area: '+str(meta.targeted_structure.values[0]+', depth: '+str(meta.imaging_depth.values[0]))
plt.suptitle(title, x=0.5, y=1.0, horizontalalignment='center')
fig_title = str(meta.ophys_experiment_id.values[0])+'_'+str(cell_specimen_id)+'_'+str(meta.targeted_structure.values[0]+'_'+str(meta.imaging_depth.values[0]))                                                                                                          
sf.save_figure(fig, figsize, save_dir, 'mean_response_oddball_transition_events', str(cell_specimen_id))
# plt.close()
In [81]:
plt.plot(oddball_df.trace.mean())
Out[81]:
[<matplotlib.lines.Line2D at 0x1da5153dba8>]
In [82]:
plt.plot(transition_df.trace.mean())
Out[82]:
[<matplotlib.lines.Line2D at 0x1da02e09a20>]

plot for all

In [ ]:
# save_dir = r'\\allen\programs\braintv\workgroups\nc-ophys\opc\opc_analysis\summary_figures'
# image_ids = sequence_images+oddball_images

# for experiment_id in expt_ids: 
#     print(experiment_id)
#     dataset = OpenScopePredictiveCodingDataset(experiment_id, cache_dir)
#     analysis = ResponseAnalysis(dataset, preload_response_dfs=False)
#     oddball_df = analysis.get_response_df('oddball')
#     transition_df = analysis.get_response_df('transition_control')
#     cell_specimen_ids = dataset.get_cell_specimen_ids()
#     meta = dataset.metadata

#     for cell_specimen_id in cell_specimen_ids: 
#         figsize = (25,6)
#         fig, ax = plt.subplots(2, len(image_ids), figsize=figsize, sharey=True)
#         ax = ax.ravel()
#         for i,image_id in enumerate(image_ids): 
#             df = oddball_df[(oddball_df.cell_specimen_id==cell_specimen_id)&(oddball_df.image_id==image_id)]
#             ax[i] = sf.plot_mean_trace(df.dff_trace.values, 31., ylabel='dF/F', legend_label=None, color='k', interval_sec=1,
#                                                  xlims=[-2, 2], ax=ax[i])
#             fraction_sig_trials = odf[(odf.cell_specimen_id==cell_specimen_id)&(odf.image_id==image_id)].fraction_significant_trials.values[0]
#             ax[i].set_title(str(int(image_id))+', '+str(np.round(fraction_sig_trials,2)))
#             ax[i].set_ylabel('')
#             ax[i].axvline(x=2*31, ymin=0, ymax=1, color=sns.color_palette()[0], linestyle='--')

#             df = transition_df[(transition_df.cell_specimen_id==cell_specimen_id)&(transition_df.image_id==image_id)&(transition_df.second_in_sequence==True)]
#             ax[i+len(image_ids)] = sf.plot_mean_trace(df.dff_trace.values, 31., ylabel='dF/F', legend_label=None, color='k', interval_sec=1,
#                                                  xlims=[-2, 2], ax=ax[i+len(image_ids)])
#             fraction_sig_trials = tdf[(tdf.cell_specimen_id==cell_specimen_id)&(tdf.image_id==image_id)].fraction_significant_trials.values[0]
#             ax[i+len(image_ids)].set_title(str(int(image_id))+', '+str(np.round(fraction_sig_trials,2)))
#             ax[i+len(image_ids)].set_ylabel('')
#             ax[i+len(image_ids)].axvline(x=2*31, ymin=0, ymax=1, color=sns.color_palette()[0], linestyle='--')  

#         ax[0].set_ylabel('dF/F')
#         ax[len(image_ids)].set_ylabel('dF/F')
#         fig.tight_layout()
#         plt.gcf().subplots_adjust(top=0.85)
#         title = 'cell_specimen_id: '+str(cell_specimen_id)+', experiment_id: '+str(meta.ophys_experiment_id.values[0])+', area: '+str(meta.targeted_structure.values[0]+', depth: '+str(meta.imaging_depth.values[0]))
#         plt.suptitle(title, x=0.5, y=1.0, horizontalalignment='center')
#         fig_title = str(meta.ophys_experiment_id.values[0])+'_'+str(cell_specimen_id)+'_'+str(meta.targeted_structure.values[0]+'_'+str(meta.imaging_depth.values[0]))                                                                                                          
#         sf.save_figure(fig, figsize, save_dir, 'mean_response_oddball_transition', fig_title)
#         plt.close()

plot variability

for one

In [100]:
cell_specimen_id = cell_specimen_ids[0] 
figsize = (25,6)
fig, ax = plt.subplots(2, len(image_ids), figsize=figsize, sharey=True)
ax = ax.ravel()
for i,image_id in enumerate(image_ids): 
    df = oddball_df[(oddball_df.cell_specimen_id==cell_specimen_id)&(oddball_df.image_id==image_id)]
    ax[i] = plot_mean_trace_with_variability(df.trace.values, 31., ylabel='dF/F', label=None, color='k', interval_sec=1,
                                         xlims=[-0.5, 0.5], ax=ax[i])
    fraction_sig_trials = odf[(odf.cell_specimen_id==cell_specimen_id)&(odf.image_id==image_id)].fraction_significant_trials.values[0]
    ax[i].set_title(str(int(image_id))+', '+str(np.round(fraction_sig_trials,2)))
    ax[i].set_ylabel('')
    ax[i].axvline(x=2*31, ymin=0, ymax=1, color=sns.color_palette()[0], linestyle='--')

    df = transition_df[(transition_df.cell_specimen_id==cell_specimen_id)&(transition_df.image_id==image_id)&(transition_df.second_in_sequence==True)]
    ax[i+len(image_ids)] = plot_mean_trace_with_variability(df.trace.values, 31., ylabel='dF/F', label=None, color='k', interval_sec=1,
                                         xlims=[-0.5, 0.5], ax=ax[i+len(image_ids)])
    fraction_sig_trials = tdf[(tdf.cell_specimen_id==cell_specimen_id)&(tdf.image_id==image_id)].fraction_significant_trials.values[0]
    ax[i+len(image_ids)].set_title(str(int(image_id))+', '+str(np.round(fraction_sig_trials,2)))
    ax[i+len(image_ids)].set_ylabel('')
    ax[i+len(image_ids)].axvline(x=2*31, ymin=0, ymax=1, color=sns.color_palette()[0], linestyle='--')  

ax[0].set_ylabel('dF/F')
ax[len(image_ids)].set_ylabel('dF/F')
fig.tight_layout()
plt.gcf().subplots_adjust(top=0.85)
title = 'cell_specimen_id: '+str(cell_specimen_id)+', experiment_id: '+str(meta.ophys_experiment_id.values[0])+', area: '+str(meta.targeted_structure.values[0]+', depth: '+str(meta.imaging_depth.values[0]))
plt.suptitle(title, x=0.5, y=1.0, horizontalalignment='center')
fig_title = str(meta.ophys_experiment_id.values[0])+'_'+str(cell_specimen_id)+'_'+str(meta.targeted_structure.values[0])+'_'+str(meta.imaging_depth.values[0])                                                                                                          
sf.save_figure(fig, figsize, save_dir, 'variability_events', fig_title)
# plt.close()

plot for all

In [ ]:
# save_dir = r'\\allen\programs\braintv\workgroups\nc-ophys\opc\opc_analysis\summary_figures'
# image_ids = sequence_images+oddball_images

# for experiment_id in expt_ids: 
#     print(experiment_id)
#     dataset = OpenScopePredictiveCodingDataset(experiment_id, cache_dir)
#     analysis = ResponseAnalysis(dataset, preload_response_dfs=False)
#     oddball_df = analysis.get_response_df('oddball')
#     transition_df = analysis.get_response_df('transition_control')
#     cell_specimen_ids = dataset.get_cell_specimen_ids()
#     meta = dataset.metadata

#     for cell_specimen_id in cell_specimen_ids: 
#         figsize = (25,6)
#         fig, ax = plt.subplots(2, len(image_ids), figsize=figsize, sharey=True)
#         ax = ax.ravel()
#         for i,image_id in enumerate(image_ids): 
#             df = oddball_df[(oddball_df.cell_specimen_id==cell_specimen_id)&(oddball_df.image_id==image_id)]
#             ax[i] = plot_mean_trace_with_variability(df.dff_trace.values, 31., ylabel='dF/F', label=None, color='k', interval_sec=1,
#                                                  xlims=[-2, 2], ax=ax[i])
#             fraction_sig_trials = odf[(odf.cell_specimen_id==cell_specimen_id)&(odf.image_id==image_id)].fraction_significant_trials.values[0]
#             ax[i].set_title(str(int(image_id))+', '+str(np.round(fraction_sig_trials,2)))
#             ax[i].set_ylabel('')
#             ax[i].axvline(x=2*31, ymin=0, ymax=1, color=sns.color_palette()[0], linestyle='--')

#             df = transition_df[(transition_df.cell_specimen_id==cell_specimen_id)&(transition_df.image_id==image_id)&(transition_df.second_in_sequence==True)]
#             ax[i+len(image_ids)] = plot_mean_trace_with_variability(df.dff_trace.values, 31., ylabel='dF/F', label=None, color='k', interval_sec=1,
#                                                  xlims=[-2, 2], ax=ax[i+len(image_ids)])
#             fraction_sig_trials = tdf[(tdf.cell_specimen_id==cell_specimen_id)&(tdf.image_id==image_id)].fraction_significant_trials.values[0]
#             ax[i+len(image_ids)].set_title(str(int(image_id))+', '+str(np.round(fraction_sig_trials,2)))
#             ax[i+len(image_ids)].set_ylabel('')
#             ax[i+len(image_ids)].axvline(x=2*31, ymin=0, ymax=1, color=sns.color_palette()[0], linestyle='--')  

#         ax[0].set_ylabel('dF/F')
#         ax[len(image_ids)].set_ylabel('dF/F')
#         fig.tight_layout()
#         plt.gcf().subplots_adjust(top=0.85)
#         title = 'cell_specimen_id: '+str(cell_specimen_id)+', experiment_id: '+str(meta.ophys_experiment_id.values[0])+', area: '+str(meta.targeted_structure.values[0]+', depth: '+str(meta.imaging_depth.values[0]))
#         plt.suptitle(title, x=0.5, y=1.0, horizontalalignment='center')
#         fig_title = str(meta.ophys_experiment_id.values[0])+'_'+str(cell_specimen_id)+'_'+str(meta.targeted_structure.values[0]+'_'+str(meta.imaging_depth.values[0])                                                                                                          
#         sf.save_figure(fig, figsize, save_dir, 'variability', fig_title)
#         plt.close()
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

population averages for different conditions

In [217]:
def get_xticks_xticklabels(trace, frame_rate, interval_sec=1, window=[-2,2]):
    """
    Function that accepts a timeseries, evaluates the number of points in the trace, and converts from acquisition frames to timestamps

    :param trace: a single trace where length = the number of timepoints
    :param frame_rate: ophys frame rate if plotting a calcium trace, stimulus frame rate if plotting running speed
    :param interval_sec: interval in seconds in between labels

    :return: xticks, xticklabels = xticks in frames corresponding to timepoints in the trace, xticklabels in seconds
    """
    interval_frames = interval_sec * frame_rate
    n_frames = len(trace)
    n_sec = n_frames / frame_rate
    xticks = np.arange(0, n_frames + 5, interval_frames)
    xticklabels = np.arange(0, n_sec + 0.1, interval_sec)
    xticklabels = xticklabels + window[0]
    if interval_sec >= 1:
        xticklabels = [int(x) for x in xticklabels]
    return xticks, xticklabels
In [218]:
def plot_response_across_conditions_population(df, condition='area', conditions=['VISp', 'VISpm', 'RSP'],
                                               window=[-2, 2], save_figures=False, colors=None, autoscale=False,
                                               title='',save_dir=None, folder=None, ax=None, pref_stim=False, frame_rate=31.):
    if pref_stim:
        df = df[df.pref_stim == True].copy()
    if ax is None:
        figsize = (5, 5)
        fig, ax = plt.subplots(figsize=figsize)
    for c, condition_value in enumerate(conditions):
        tmp = df[df[condition] == condition_value]
        if colors is None:
            colors = sns.color_palette()
        traces = tmp.mean_trace.values
        trace = np.mean(traces, axis=0)
        ax = sf.plot_mean_trace(traces, frame_rate, legend_label=condition_value, color=colors[c],
                                interval_sec=0.5, xlims=window, ax=ax)
        
    ax.axvspan(np.abs(window[0])*frame_rate, (np.abs(window[0])+0.25)*frame_rate, 
               facecolor='gray', edgecolor='none', alpha=0.7, linewidth=0, zorder=1)
    
#     ax = plot_flashes_on_trace(ax, flashes=False, alpha=0.15, window=window, omitted=True)
    
    xticks, xticklabels = get_xticks_xticklabels(trace, 31., interval_sec=1, window=window)
    ax.set_xticks(xticks)
    ax.set_xticklabels([int(x) for x in xticklabels])
    #     ax.set_xlim(0,(np.abs(window[0])+window[1])*31.)
    ax.legend(bbox_to_anchor=(1.1, 1), title=condition)
    if not autoscale:
        ymin, ymax = ax.get_ylim()
        if ymin > 0:
            ax.set_ylim(0, ymax * 1.2)
        else:
            ax.set_ylim(ymin * 1.2, ymax * 1.2)
    ax.set_title(title)
    if save_figures:
        fig.tight_layout()
        save_figure(fig, figsize, save_dir, folder, 'population_response_across_' + condition + '_' + cre_line)
    return ax
In [219]:
areas = ['VISp','VISpm','RSP']
In [220]:
odf['expected'] = np.nan
In [221]:
expected_image = analysis.get_sequence_images()[3]
oddball_images = analysis.get_oddball_images()
In [222]:
expected_list = []
for image_id in odf.image_id.values: 
    if image_id == expected_image: 
        expected_list.append(True)
    elif image_id in oddball_images:
        expected_list.append(False)
    else: 
        expected_list.append(np.nan)
odf['expected'] = expected_list
In [223]:
odf.expected.unique()
Out[223]:
array([False, nan, True], dtype=object)
In [108]:
odf[['image_id', 'oddball', 'expected']]
Out[108]:
image_id oddball expected
0 6.0 True False
1 13.0 False NaN
2 17.0 True False
3 22.0 True False
4 26.0 False True
... ... ... ...
90589 89.0 True False
90590 103.0 True False
90591 110.0 True False
90592 111.0 True False
90593 112.0 True False

85036 rows × 3 columns

In [109]:
experiment_id = 828956958
fig, ax = plt.subplots()
df = odf[(odf.experiment_id==experiment_id)].copy()
plot_response_across_conditions_population(df, condition='expected', conditions=[True, False],
                                                   window=[-2, 2], ax=ax)
Out[109]:
<matplotlib.axes._subplots.AxesSubplot at 0x1dc2edf7ef0>
In [110]:
c = sns.color_palette()
colors = [c[0], c[3]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='expected', conditions=[True, False],
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
In [111]:
c = sns.color_palette()
colors = [c[0], c[3]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)&(odf.retrogradely_labeled==True)].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='expected', conditions=[True, False],
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
plt.suptitle('retrogradely labeled')
Out[111]:
Text(0.5, 0.98, 'retrogradely labeled')
In [112]:
c = sns.color_palette()
colors = [c[0], c[3]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)&(odf.retrogradely_labeled==False)].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='expected', conditions=[True, False],
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
plt.suptitle('not retrogradely labeled')
Out[112]:
Text(0.5, 0.98, 'not retrogradely labeled')
In [1]:
c = sns.color_palette()
colors = [c[3], c[2]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)&(odf.oddball==True)].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='retrogradely_labeled', conditions=[True, False],
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
plt.suptitle('oddball response')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-bccdeff0018f> in <module>
----> 1 c = sns.color_palette()
      2 colors = [c[3], c[2]]
      3 figsize = (15,5)
      4 fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
      5 for i,area in enumerate(areas):

NameError: name 'sns' is not defined
In [130]:
c = sns.color_palette()
colors = [c[3], c[2]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)&(odf.oddball==False)&(odf.image_id==sequence_images[0])].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='retrogradely_labeled', conditions=[True, False],
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
plt.suptitle('first image in sequence - image A')
Out[130]:
Text(0.5, 0.98, 'first image in sequence - image A')
In [131]:
c = sns.color_palette()
colors = [c[3], c[2]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)&(odf.image_id==sequence_images[3])].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='retrogradely_labeled', conditions=[True, False],
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
plt.suptitle('expected sequence image - image D')
Out[131]:
Text(0.5, 0.98, 'expected sequence image - image D')
In [231]:
odf.pathway.unique()
Out[231]:
array(['FB', 'FF'], dtype=object)
In [241]:
c = sns.color_palette()
colors = [c[3], c[2]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)&(odf.oddball==True)&(odf.retrogradely_labeled==True)].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='pathway', conditions=df.pathway.unique(),
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
plt.suptitle('oddball images - retrogradely labeled')
Out[241]:
Text(0.5, 0.98, 'oddball images')
In [242]:
c = sns.color_palette()
colors = [c[3], c[2]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)&(odf.oddball==True)&(odf.retrogradely_labeled==False)].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='pathway', conditions=df.pathway.unique(),
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
plt.suptitle('oddball images - not labeled')
Out[242]:
Text(0.5, 0.98, 'oddball images - not labeled')
In [239]:
c = sns.color_palette()
colors = [c[3], c[2]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)&(odf.image_id==sequence_images[3])].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='pathway', conditions=df.pathway.unique(),
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
plt.suptitle('expected sequence image - image D')
Out[239]:
Text(0.5, 0.98, 'expected sequence image - image D')
In [ ]:
 
In [ ]:
 
In [ ]:
 

single cells

In [137]:
from openscope_predictive_coding.ophys.plotting import population_summary_figures as psf
In [ ]:
c = sns.color_palette()
colors = [c[3], c[2]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)&(odf.oddball==False)&(odf.image_id==sequence_images[0])&()].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='retrogradely_labeled', conditions=[True, False],
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
plt.suptitle('first image in sequence - image A')
In [149]:
cdf.keys()
Out[149]:
Index(['cell_specimen_id', 'image_id', 'oddball', 'mean_response',
       'sem_response', 'mean_trace', 'sem_trace', 'pref_stim',
       'fraction_significant_trials', 'fraction_responsive_trials',
       'experiment_id', 'experiment_container_id', 'targeted_structure',
       'imaging_depth', 'cre_line', 'reporter_line', 'full_genotype',
       'session_type', 'donor_id', 'experiment_date', 'specimen_id', 'depth',
       'location', 'session_num', 'area', 'retrogradely_labeled', 'expected'],
      dtype='object')
In [157]:
for cell_specimen_id in odf[(odf.targeted_structure=='RSP')&(odf.experiment_id==830688102)].cell_specimen_id.unique():
    figsize = (20,4)
    fig, ax = plt.subplots(1,5, figsize=figsize, sharey=True)
    ax = ax.ravel()
    cdf = odf[odf.cell_specimen_id==cell_specimen_id]

    for i, sequence_image in enumerate(sequence_images): 
        df = cdf[(cdf.image_id==sequence_image)].copy()
        psf.plot_mean_trace_from_mean_df(df, ax=ax[i], xlims=[-2,2])
        ax[i].axvline(x=31*2, linestyle='--')
        ax[i].set_title(sequence_image)

    i+=1
    df = cdf[(cdf.oddball==True)].copy()
    psf.plot_mean_trace_from_mean_df(df, ax=ax[i], xlims=[-2,2])
    ax[i].set_title('oddball')
    ax[i].axvline(x=31*2, linestyle='--')

    plt.suptitle(str(cdf.cell_specimen_id.values[0])+'_'+cdf.targeted_structure.values[0]+'_'+str(cdf.retrogradely_labeled.values[0]),x=0.5, y=1.05)
    fig.tight_layout()
C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\ipykernel_launcher.py:3: RuntimeWarning:

More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).

In [158]:
for cell_specimen_id in odf[(odf.targeted_structure=='VISp')].cell_specimen_id.unique()[:100]:
    figsize = (20,4)
    fig, ax = plt.subplots(1,5, figsize=figsize, sharey=True)
    ax = ax.ravel()
    cdf = odf[odf.cell_specimen_id==cell_specimen_id]

    for i, sequence_image in enumerate(sequence_images): 
        df = cdf[(cdf.image_id==sequence_image)].copy()
        psf.plot_mean_trace_from_mean_df(df, ax=ax[i], xlims=[-2,2])
        ax[i].axvline(x=31*2, linestyle='--')
        ax[i].set_title(sequence_image)

    i+=1
    df = cdf[(cdf.oddball==True)].copy()
    psf.plot_mean_trace_from_mean_df(df, ax=ax[i], xlims=[-2,2])
    ax[i].set_title('oddball')
    ax[i].axvline(x=31*2, linestyle='--')

    plt.suptitle(str(cdf.cell_specimen_id.values[0])+'_'+cdf.targeted_structure.values[0]+'_'+str(cdf.retrogradely_labeled.values[0]),x=0.5, y=1.05)
    fig.tight_layout()
C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\ipykernel_launcher.py:3: RuntimeWarning:

More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).

In [ ]:
 
In [ ]:
 

heatmaps

In [323]:
def reannotate_pref_stim(df):
    m = df.copy()
    m['pref_stim'] = False
    for cell in m.cell_specimen_id.values:
        cdf = m[m.cell_specimen_id==cell]
        image_index = np.where(cdf['mean_response'].values == np.max(cdf['mean_response'].values))[0][0]
        pref_image = cdf['mean_response'].index[image_index]
        rows = df[(df.cell_specimen_id == cell) & (df.image_id == pref_image)].index
        for trial in trials:
            df.loc[rows, 'pref_stim'] = True
    return df
In [326]:
def plot_mean_image_responses(df, image_ids, vmax=0.03, colorbar=False, ax=None, save_dir=None, folder=None, use_events=True,
                               interval_sec=1, window=[-2,2]):
    if use_events:
#         vmax = 0.003
        label = 'mean event magnitude'
        suffix = '_events'
    else:
        # vmax = 0.3
        label = 'mean dF/F'
        suffix = ''
    experiment_id = df.experiment_id.unique()[0]
    imaging_area = df.area.unique()[0]
    
    
    df = reannotate_pref_stim(df[df.image_id.isin(image_ids)])
    
    figsize = (25,10)
    cells = []
    for image_id in image_ids:
        tmp = df[(df.image_id == image_id) & (df.pref_stim == True)]
        order = np.argsort(tmp.mean_response.values)[::-1]
        cell_ids = list(tmp.cell_specimen_id.values[order])
        cells = cells + cell_ids
    if ax is None:
        fig, ax = plt.subplots(1, len(image_ids), figsize=figsize, sharey=True, sharex=True)
        ax = ax.ravel()
    for i, image_id in enumerate(image_ids):
        im_df = df[(df.image_id == image_id)]
        len_trace = len(im_df.mean_trace.values[0])
        response_array = np.empty((len(cells), len_trace))
        for x, cell in enumerate(cells):
            tmp = im_df[im_df.cell_specimen_id == cell]
            if len(tmp) >= 1:
                trace = tmp.mean_trace.values[0]
            else:
                trace = np.empty((len_trace))
                trace[:] = np.nan
            response_array[x, :] = trace
        sns.heatmap(data=response_array, vmin=0, vmax=vmax, ax=ax[i], cmap='magma', cbar=colorbar,
                    cbar_kws={'label': label})
        xticks, xticklabels = sf.get_xticks_xticklabels(trace, 31., interval_sec=interval_sec, window=window)
        ax[i].set_xticks(xticks)
        if interval_sec < 1:
            ax[i].set_xticklabels(xticklabels)
        else:
            ax[i].set_xticklabels([int(x) for x in xticklabels])
        if response_array.shape[0] > 300:
            interval = 500
        else:
            interval = 50
        ax[i].set_xlim(0, (np.abs(window[0])+window[1])*31.)
        ax[i].set_yticks(np.arange(0, response_array.shape[0], interval))
        ax[i].set_yticklabels(np.arange(0, response_array.shape[0], interval))
        ax[i].set_xlabel('time (sec)', fontsize=16)
        ax[i].set_title(str(int(image_id)))
        ax[0].set_ylabel('cells')
    plt.suptitle(str(experiment_id)+', '+imaging_area, x=0.52, y=.98, fontsize=18, horizontalalignment='center')
    fig.tight_layout()
    plt.gcf().subplots_adjust(top=0.85)
#     if save_dir:
#         save_figure(fig, figsize, save_dir, folder,
#                     'change_response_matrix_' + cre_line + '_' + image_set + '_' + suffix)
In [ ]:
 
In [ ]:
 
In [316]:
image_ids = sequence_images+oddball_images
plot_mean_image_responses(odf, image_ids, vmax=0.005, colorbar=False, ax=None, save_dir=None, folder=None, use_events=True,
                               interval_sec=1, window=[-2,2])
In [327]:
plot_mean_image_responses(odf[odf.area==area], image_ids, vmax=0.008, colorbar=False, ax=None, save_dir=None, folder=None, use_events=True,
                               interval_sec=1, window=[-2,2])
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2896             try:
-> 2897                 return self._engine.get_loc(key)
   2898             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'trace'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
~\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\groupby.py in apply(self, func, *args, **kwargs)
    724             try:
--> 725                 result = self._python_apply_general(f)
    726             except Exception:

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\groupby.py in _python_apply_general(self, f)
    741     def _python_apply_general(self, f):
--> 742         keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
    743 

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\ops.py in apply(self, f, data, axis)
    236             group_axes = _get_axes(group)
--> 237             res = f(group)
    238             if not _is_indexed_like(res, group_axes):

~\Documents\Code\openscope_predictive_coding\openscope_predictive_coding\ophys\response_analysis\utilities.py in get_mean_sem_trace(group)
     57     sem_response = np.std(group['mean_response'].values) / np.sqrt(len(group['mean_response'].values))
---> 58     mean_trace = np.mean(group['trace'])
     59     sem_trace = np.std(group['trace'].values) / np.sqrt(len(group['trace'].values))

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2979                 return self._getitem_multilevel(key)
-> 2980             indexer = self.columns.get_loc(key)
   2981             if is_integer(indexer):

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2898             except KeyError:
-> 2899                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2900         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'trace'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
~\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2896             try:
-> 2897                 return self._engine.get_loc(key)
   2898             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'trace'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-327-2702782df23d> in <module>
      1 plot_mean_image_responses(odf[odf.area==area], image_ids, vmax=0.008, colorbar=False, ax=None, save_dir=None, folder=None, use_events=True,
----> 2                                interval_sec=1, window=[-2,2])

<ipython-input-326-5faa3170b6c4> in plot_mean_image_responses(df, image_ids, vmax, colorbar, ax, save_dir, folder, use_events, interval_sec, window)
     13 
     14 
---> 15     df = reannotate_pref_stim(df[df.image_id.isin(image_ids)])
     16 
     17     figsize = (25,10)

<ipython-input-323-37f26ef10833> in reannotate_pref_stim(df)
      1 def reannotate_pref_stim(df):
----> 2     mean_response = df.groupby(['cell_specimen_id', 'image_id']).apply(ut.get_mean_sem_trace)
      3     m = mean_response.unstack()
      4     for cell in m.cell_specimen_id.values:
      5         cdf = m[m.cell_specimen_id==cell]

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\groupby.py in apply(self, func, *args, **kwargs)
    735 
    736                 with _group_selection_context(self):
--> 737                     return self._python_apply_general(f)
    738 
    739         return result

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\groupby.py in _python_apply_general(self, f)
    740 
    741     def _python_apply_general(self, f):
--> 742         keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
    743 
    744         return self._wrap_applied_output(

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\ops.py in apply(self, f, data, axis)
    235             # group might be modified
    236             group_axes = _get_axes(group)
--> 237             res = f(group)
    238             if not _is_indexed_like(res, group_axes):
    239                 mutated = True

~\Documents\Code\openscope_predictive_coding\openscope_predictive_coding\ophys\response_analysis\utilities.py in get_mean_sem_trace(group)
     56     mean_responses = group['mean_response'].values
     57     sem_response = np.std(group['mean_response'].values) / np.sqrt(len(group['mean_response'].values))
---> 58     mean_trace = np.mean(group['trace'])
     59     sem_trace = np.std(group['trace'].values) / np.sqrt(len(group['trace'].values))
     60     return pd.Series({'mean_response': mean_response, 'sem_response': sem_response,

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2978             if self.columns.nlevels > 1:
   2979                 return self._getitem_multilevel(key)
-> 2980             indexer = self.columns.get_loc(key)
   2981             if is_integer(indexer):
   2982                 indexer = [indexer]

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2897                 return self._engine.get_loc(key)
   2898             except KeyError:
-> 2899                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2900         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2901         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'trace'
In [322]:
image_ids = sequence_images
for area in odf.area.unique():
    plot_mean_image_responses(odf[odf.area==area], image_ids, vmax=0.008, colorbar=False, ax=None, save_dir=None, folder=None, use_events=True,
                               interval_sec=1, window=[-2,2])
In [321]:
image_ids = sequence_images

for experiment_id in odf.experiment_id.unique():
    try:
        plot_mean_image_responses(odf[odf.experiment_id==experiment_id], image_ids, vmax=0.008, colorbar=False, ax=None, save_dir=None, folder=None, use_events=True,
                               interval_sec=1, window=[-2,2])
    except: 
        print(experiment_id)
C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\seaborn\matrix.py:287: UserWarning:

Attempting to set identical bottom == top == 0 results in singular transformations; automatically expanding.

775058863
C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\seaborn\matrix.py:287: UserWarning:

Attempting to set identical bottom == top == 0 results in singular transformations; automatically expanding.

830688059
C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\ipykernel_launcher.py:23: RuntimeWarning:

More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).

C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\seaborn\matrix.py:287: UserWarning:

Attempting to set identical bottom == top == 0 results in singular transformations; automatically expanding.

832601977
C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\seaborn\matrix.py:287: UserWarning:

Attempting to set identical bottom == top == 0 results in singular transformations; automatically expanding.

833599179
C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\seaborn\matrix.py:287: UserWarning:

Attempting to set identical bottom == top == 0 results in singular transformations; automatically expanding.

833614835
C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\seaborn\matrix.py:287: UserWarning:

Attempting to set identical bottom == top == 0 results in singular transformations; automatically expanding.

834260382
C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\seaborn\matrix.py:287: UserWarning:

Attempting to set identical bottom == top == 0 results in singular transformations; automatically expanding.

836906598
C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\seaborn\matrix.py:287: UserWarning:

Attempting to set identical bottom == top == 0 results in singular transformations; automatically expanding.

837285285
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [133]:
c = sns.color_palette()
colors = [c[0], c[3]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)&(odf.depth=='superficial')].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='expected', conditions=[True, False],
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
plt.suptitle('superficial')
Out[133]:
Text(0.5, 0.98, 'superficial')
In [134]:
c = sns.color_palette()
colors = [c[0], c[3]]
figsize = (15,5)
fig, ax = plt.subplots(1,3, figsize=figsize, sharey=True)
for i,area in enumerate(areas):
    df = odf[(odf.area==area)&(odf.depth=='deep')].copy()
    ax[i] = plot_response_across_conditions_population(df, condition='expected', conditions=[True, False],
                                                   window=[-2, 2], ax=ax[i], colors=colors, title=area)
plt.suptitle('deep')
Out[134]:
Text(0.5, 0.98, 'deep')
In [132]:
odf
Out[132]:
cell_specimen_id image_id oddball mean_response sem_response mean_trace sem_trace pref_stim fraction_significant_trials fraction_responsive_trials ... session_type donor_id experiment_date specimen_id depth location session_num area retrogradely_labeled expected
0 768915941 6.0 True 0.000015 0.000014 [8.50968698148599e-11, 3.720238322480506e-12, ... [7.872724620843885e-11, 3.460640432655818e-12,... False 0.000000 0.000000 ... behavior_session_1 416366 2018-10-25 753787399 superficial RSP_superficial 1 RSP False False
1 768915941 13.0 False 0.001431 0.000120 [0.0008259387979521318, 0.0008112338215326325,... [0.00015156177494914525, 0.0001541630660198347... False 0.020500 0.000000 ... behavior_session_1 416366 2018-10-25 753787399 superficial RSP_superficial 1 RSP False NaN
2 768915941 17.0 True 0.000980 0.000924 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... False 0.000000 0.000000 ... behavior_session_1 416366 2018-10-25 753787399 superficial RSP_superficial 1 RSP False False
3 768915941 22.0 True 0.006094 0.004962 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... False 0.100000 0.000000 ... behavior_session_1 416366 2018-10-25 753787399 superficial RSP_superficial 1 RSP False False
4 768915941 26.0 False 0.002497 0.000217 [0.0007811450115317037, 0.0005789902713344787,... [0.00013984678227011888, 0.0001020979018990593... False 0.038947 0.001053 ... behavior_session_1 416366 2018-10-25 753787399 superficial RSP_superficial 1 RSP False True
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
90589 848731002 89.0 True 0.001622 0.001539 [2.4095915992731943e-06, 2.8778467701174954e-0... [2.285939305353306e-06, 2.7301651651590845e-07... False 0.100000 0.000000 ... behavior_session_2 437813 2019-04-09 827598535 deep VISp_deep 2 VISp False False
90590 848731002 103.0 True 0.003441 0.002200 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.003... [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.002... False 0.100000 0.000000 ... behavior_session_2 437813 2019-04-09 827598535 deep VISp_deep 2 VISp False False
90591 848731002 110.0 True 0.005878 0.002769 [3.6930439211679226e-06, 4.4107113425942047e-0... [3.5035284998391786e-06, 4.1843680267736185e-0... False 0.100000 0.000000 ... behavior_session_2 437813 2019-04-09 827598535 deep VISp_deep 2 VISp False False
90592 848731002 111.0 True 0.003921 0.002501 [2.6500953437269025e-12, 9.068127600884243e-14... [2.514101190835148e-12, 8.602781199549755e-14,... False 0.100000 0.000000 ... behavior_session_2 437813 2019-04-09 827598535 deep VISp_deep 2 VISp False False
90593 848731002 112.0 True 0.000645 0.000612 [9.030897443867194e-21, 8.853580320355909e-23,... [8.567461571403885e-21, 8.399243777870366e-23,... False 0.000000 0.000000 ... behavior_session_2 437813 2019-04-09 827598535 deep VISp_deep 2 VISp False False

85036 rows × 27 columns

oddball response by session

In [135]:
figsize=(20,20)
fig, ax = plt.subplots(7,8,figsize=figsize, sharex=True, sharey=True)
ax = ax.ravel()
i=0

for experiment_id in odf.experiment_id.unique(): 
    edf = odf[odf.experiment_id==experiment_id]   
    sequence = edf[edf.image_id==images[3]].mean_trace.mean()
    oddballs = edf[edf.oddball==True].mean_trace.mean()

    ax[i].plot(sequence, color='blue', label='sequence')
    ax[i].plot(oddballs, color='red', label='oddballs')

    xticks, xticklabels = sf.get_xticks_xticklabels(sequence, 31., interval_sec=1)
    ax[i].set_xticks(xticks)
    ax[i].set_xticklabels([int(x) for x in xticklabels])
    ax[i].set_ylabel('dF/F')
    ax[i].set_xlabel('time (sec)')
    ax[i].set_title(str(experiment_id)+'_'+str(edf.donor_id.values[0])+'\n'+edf.targeted_structure.values[0]+', '+str(edf.imaging_depth.values[0]))
    i+=1
fig.tight_layout()
In [ ]:
 
In [ ]:
 

oddball response metric - transitions control

In [104]:
odf = odf.reset_index()

oddball_index = []
for index in odf.index.values: 
    row_df = odf.iloc[index]
    cell_specimen_id = row_df.cell_specimen_id
    image_id = row_df.image_id
    oddball_mean_response = row_df.mean_response
    tcdf = tdf[(tdf.image_id==image_id)&(tdf.cell_specimen_id==cell_specimen_id)&(tdf.second_in_sequence==True)]
    transitions_mean_response = tcdf.mean_response.values[0]
    oddball_response_index = ((oddball_mean_response-transitions_mean_response)/(oddball_mean_response+transitions_mean_response))
    oddball_index.append(oddball_response_index)
odf['oddball_response_index'] = oddball_index
In [43]:
def plot_oddball_transition_responses(odf, tdf, cell_specimen_id, save_dir=None):
    images = oddball_images+sequence_images
    figsize=(20,2.5)
    fig, ax = plt.subplots(1, len(images), figsize=figsize, sharey=True)
    for i,image_id in enumerate(images): 
        cell_odf = odf[(odf.cell_specimen_id==cell_specimen_id)&(odf.image_id==image_id)]
        cell_tdf = tdf[(tdf.cell_specimen_id==cell_specimen_id)&(tdf.image_id==image_id)]
        ax[i].plot(cell_odf.mean_trace.values[0], color=sns.color_palette()[3], label='oddball_block')
        ax[i].plot(cell_tdf.mean_trace.values[0], color=sns.color_palette()[0], label='transition_control')
        xticks, xticklabels = sf.get_xticks_xticklabels(cell_odf.mean_trace.values[0], 31.)
        ax[i].set_xticks(xticks)
        ax[i].set_xticklabels(xticklabels)
        ax[i].set_xlabel('time (s)')
        ax[i].set_title(str(np.round(cell_odf.oddball_response_index.values[0], 2))+', '+str(np.round(cell_odf.fraction_significant_trials.values[0], 2)),fontsize=12)
    ax[0].set_ylabel('dF/F')
    ax[i].legend(fontsize='small')
    fig.tight_layout()
    fig.subplots_adjust(bottom=0.25)
    if save_dir: 
        sf.save_figure(fig, figsize, save_dir, 'oddball_transition_responses', str(int(cell_specimen_id)))
        plt.close()
In [44]:
cell_specimen_id = odf.cell_specimen_id.unique()[0]

plot_oddball_transition_responses(odf, tdf, cell_specimen_id, save_dir=None)
C:\Users\marinag\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\ipykernel_launcher.py:17: UserWarning: Tight layout not applied. tight_layout cannot make axes width small enough to accommodate all axes decorations
In [45]:
# for cell_specimen_id in odf.cell_specimen_id.unique():
#     plot_oddball_transition_responses(odf, tdf, cell_specimen_id, save_dir=save_dir)
In [151]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = odf.copy()
df = df[df.oddball==True]
df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
df = df[df.fraction_significant_trials>0.1]
colors = [sns.color_palette()[0], sns.color_palette()[3]]

fig, ax = plt.subplots()
ax = sns.pointplot(data=df, x='location', y='oddball_response_index', order=locations, 
                   hue='retrogradely_labeled', palette=colors, ax=ax)
ax.set_xticklabels(locations, rotation=90)
ax.set_xlabel('')
ax.set_ylim(-0.02, 0.8)
legend = ax.legend(fontsize='x-small', title='retrogradely labeled', loc='upper right')
plt.setp(legend.get_title(),fontsize='x-small')
Out[151]:
[None, None]
In [155]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

metric = 'oddball_response_index'

df = odf.copy()
df = df[df.oddball==True]
df = df[(df[metric]>-5)&(df[metric]<5)]
df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
df = df[df.fraction_significant_trials>0.01]
colors = [sns.color_palette()[0], sns.color_palette()[3], sns.color_palette()[2]]

fig, ax = plt.subplots(1, figsize=(5,5), sharey=True, sharex=True)
for i, area in enumerate(['VISp', 'VISpm', 'RSP']):
    ax = sns.distplot(df[(df.area==area)&(df.retrogradely_labeled==label)][metric].values, bins=30,
                  color=colors[i], ax=ax, label=area)
    ax.set_xlabel(metric)
    legend = ax.legend(fontsize='x-small', title='area', loc='upper right')
    plt.setp(legend.get_title(),fontsize='x-small')
    ax.set_ylabel('density')
#     ax.set_title(area)
In [159]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

metric = 'oddball_response_index'

df = odf.copy()
df = df[df.oddball==True]
df = df[(df[metric]>-5)&(df[metric]<5)]
df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
df = df[df.fraction_significant_trials>0.01]
colors = [sns.color_palette()[0], sns.color_palette()[3]]

fig, ax = plt.subplots(1,3, figsize=(15,5), sharey=True, sharex=True)
for i, area in enumerate(['VISp', 'VISpm', 'RSP']):
    for c, label in enumerate(df.retrogradely_labeled.unique()):
        ax[i] = sns.distplot(df[(df.area==area)&(df.retrogradely_labeled==label)][metric].values, bins=30,
                      color=colors[c], ax=ax[i], label=label)
        ax[i].set_xlabel(metric)
        legend = ax[i].legend(fontsize='x-small', title='retrogradely labeled', loc='upper right')
        plt.setp(legend.get_title(),fontsize='x-small')
        ax[i].set_title(area)
ax[0].set_ylabel('density')
Out[159]:
Text(0, 0.5, 'density')
In [158]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

metric = 'oddball_response_index'

df = odf.copy()
df = df[df.oddball==True]
df = df[(df[metric]>-5)&(df[metric]<5)]
df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
df = df[df.fraction_significant_trials>0.1]

fig, ax = plt.subplots(1,3, figsize=(15,5), sharey=True, sharex=True)
for i, area in enumerate(['VISp', 'VISpm', 'RSP']):
    for c, depth in enumerate(df.depth.unique()):
        ax[i] = sns.distplot(df[(df.area==area)&(df.depth==depth)][metric].values, bins=30,
                      color=colors[c], ax=ax[i], label=depth)
        ax[i].set_xlabel(metric)
        ax[i].legend()
        ax[i].set_title(area)
ax[0].set_ylabel('density')
Out[158]:
Text(0, 0.5, 'density')
In [49]:
metric = 'oddball_response_index'

df = odf.copy()
df = df[df.oddball==True]
df = df[(df[metric]>-5)&(df[metric]<5)]
df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
# df = df[df.fraction_significant_trials>0.0]

fig, ax = plt.subplots(1,2, figsize=(10,5), sharey=True, sharex=True)
for i, depth in enumerate(df.depth.unique()):
    for c, label in enumerate(df.retrogradely_labeled.unique()):
        ax[i] = sns.distplot(df[(df.depth==depth)&(df.retrogradely_labeled==label)][metric].values, bins=30,
                      color=colors[c], ax=ax[i], label=label)
        ax[i].set_xlabel(metric)
        ax[i].legend()
        ax[i].set_title(depth)
In [50]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = odf.copy()
df = df[df.oddball==True]
# df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
# df = df[df.fraction_significant_trials>0.0]

fig, ax = plt.subplots()
ax = sns.distplot(data=df, x='location', y='oddball_response_index', order=locations, hue='retrogradely_labeled', ax=ax)
ax.set_xticklabels(locations, rotation=90)
ax.set_xlabel('')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-6cbd482da10f> in <module>
      8 
      9 fig, ax = plt.subplots()
---> 10 ax = sns.distplot(data=df, x='location', y='oddball_response_index', order=locations, hue='retrogradely_labeled', ax=ax)
     11 ax.set_xticklabels(locations, rotation=90)
     12 ax.set_xlabel('')

TypeError: distplot() got an unexpected keyword argument 'data'

oddball response metric - randomized control

In [ ]:
odf = odf.reset_index()

oddball_index = []
for index in odf.index.values: 
    row_df = odf.iloc[index]
    cell_specimen_id = row_df.cell_specimen_id
    image_id = row_df.image_id
    oddball_mean_response = row_df.mean_response
    pre_cdf = pre_df[(pre_df.image_id==image_id)&(pre_df.cell_specimen_id==cell_specimen_id)]
    control_mean_response = pre_cdf.mean_response.values[0]
    oddball_response_index = ((oddball_mean_response-control_mean_response)/(oddball_mean_response+control_mean_response))
    oddball_index.append(oddball_response_index)
odf['oddball_response_index_control'] = oddball_index
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

occlusion metric

In [174]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = ocdf.copy()
# df = df[df.oddball==True]
df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
# df = df[df.fraction_significant_trials>0.1]
cells = df[df.fraction_significant_trials>0.1].cell_specimen_id.unique()
df = df[df.cell_specimen_id.isin(cells)]
colors = [sns.color_palette()[0], sns.color_palette()[3]]

fig, ax = plt.subplots()
ax = sns.pointplot(data=df, x='location', y='mean_response', order=locations, hue='fraction_occlusion', 
                   palette=sns.color_palette('Blues'), ax=ax)
ax.set_xticklabels(locations, rotation=90)
ax.set_xlabel('')
ax.legend()
# ax.set_ylim(0,0.8)
ax.set_title('occlusion population response')
Out[174]:
Text(0.5, 1.0, 'occlusion population response')
In [173]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = ocdf.copy()
# df = df[df.oddball==True]
df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
cells = df[df.fraction_significant_trials>0.1].cell_specimen_id.unique()
df = df[df.cell_specimen_id.isin(cells)]
colors = [sns.color_palette()[0], sns.color_palette()[3]]

fig, ax = plt.subplots()
ax = sns.pointplot(data=df, x='fraction_occlusion', y='mean_response', order=np.sort(df.fraction_occlusion.unique()),
                   hue='location', hue_order=locations, palette=sns.color_palette(), ax=ax)
ax.set_xticklabels(np.sort(df.fraction_occlusion.unique()))
ax.set_xlabel('fraction occlusion')
# ax.set_ylim(0,0.8)
ax.set_title('occlusion tuning')
legend = ax.legend(fontsize='x-small', title='location', bbox_to_anchor=(1,1))
plt.setp(legend.get_title(),fontsize='x-small')
Out[173]:
[None, None]
In [ ]:
 
In [166]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = ocdf.copy()
# df = df[df.oddball==True]
df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
# df = df[df.fraction_significant_trials>0.1]
colors = [sns.color_palette()[0], sns.color_palette()[3]]

fig, ax = plt.subplots()
ax = sns.pointplot(data=df, x='location', y='mean_response', order=locations, hue='retrogradely_labeled', 
                   palette=colors, ax=ax)
ax.set_xticklabels(locations, rotation=90)
ax.set_xlabel('')
ax.set_ylim(0,0.6)
legend = ax.legend(fontsize='x-small', title='retrogradely labeled', loc='upper right')
plt.setp(legend.get_title(),fontsize='x-small')
ax.set_title('occlusion population response')
Out[166]:
Text(0.5, 1.0, 'occlusion population response')

need to calculate the pref amount of occlusion

In [ ]:
 
In [ ]:
 
In [ ]:
# locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

# df = ocdf.copy()
# # df = df[df.oddball==True]
# # df = df[df.pref_stim==True]
# # df = df[df.mean_response>0.05]
# colors = sns.color_palette('Blues')

# fig, ax = plt.subplots()
# for i, location in enumerate(locations): 
#     tmp = df[df.location==location]
#     for r, occ in enumerate(tmp.fraction_occlusion.unique()):
#         fraction_pref = len(tmp[(tmp.pref_stim==True)&(tmp.fraction_occlusion==occ)])/float(len(tmp.cell_specimen_id.unique()))
#         ax.plot(i, fraction_pref, 'o', color=colors[r])
# for r, occ in enumerate(tmp.fraction_occlusion.unique()):
#     fraction_pref = len(tmp[(tmp.pref_stim==True)&(tmp.fraction_occlusion==occ)])/float(len(tmp.cell_specimen_id.unique()))
#     ax.plot(i, fraction_pref, 'o', color=colors[r], label=occ)
# ax.set_xticks(range(len(locations)))
# ax.set_xticklabels(locations, rotation=90)
# ax.set_xlabel('')
# ax.set_ylim(-0.05,1.05)
# legend = ax.legend(fontsize='x-small', title='fraction occlusion', loc='upper right')
# plt.setp(legend.get_title(),fontsize='x-small')
# ax.set_ylabel('fraction responsive cells')
# ax.set_title('occlusion responsiveness');
In [ ]:
 
In [ ]:
 
In [213]:
def occlusion_response_index_byimage(dfc, val='mean_response'):
    dfc_occlusion = dfc[dfc['fraction_occlusion']!=0]
    dfc_noocclusion = dfc[dfc['fraction_occlusion']==0]
    occlusion_zscore = abs(dfc_occlusion.groupby('image_id')[val].mean()-dfc_noocclusion.groupby('image_id')[val].mean())/dfc_occlusion.groupby('image_id')[val].std()
    return occlusion_zscore 

def occlusion_response_index_bycell(dfc, val='mean_response'):
#     dfc = dfc[dfc.pref_stim==True]
    dfc_occlusion = dfc[dfc['fraction_occlusion']!=0]
    dfc_noocclusion = dfc[dfc['fraction_occlusion']==0]
    occlusion_zscore = abs(dfc_occlusion.groupby(['cell_specimen_id', 'image_id'])[val].mean()-dfc_noocclusion.groupby(['cell_specimen_id', 'image_id'])[val].mean())/dfc_occlusion.groupby(['cell_specimen_id', 'image_id'])[val].std()
    return occlusion_zscore 
In [214]:
occlusion_values = occlusion_response_index_bycell(ocdf, val='mean_response')
In [215]:
oc = pd.DataFrame(occlusion_values)
oc = oc.rename(columns={'mean_response':'occlusion_index_zscore'})
In [220]:
metric_df = pd.merge(odf, oc, left_on=['cell_specimen_id', 'image_id'], right_on=['cell_specimen_id', 'image_id'])
In [ ]:
 
In [222]:
df = metric_df.copy()
df = df[df.pref_stim==True]
df = df[(df.oddball_response_index<2)&(df.occlusion_index_zscore<40)]
sns.scatterplot(data=df, x='occlusion_index_zscore', y='oddball_response_index')
Out[222]:
<matplotlib.axes._subplots.AxesSubplot at 0x1b342e301d0>
In [236]:
ocdf_metric = pd.merge(ocdf, oc, left_on=['cell_specimen_id', 'image_id'], right_on=['cell_specimen_id', 'image_id'])
In [237]:
ocdf_metric
Out[237]:
cell_specimen_id image_id fraction_occlusion mean_response sem_response mean_trace sem_trace pref_stim fraction_significant_trials fraction_responsive_trials ... session_type donor_id experiment_date specimen_id session_num area depth location retrogradely_labeled occlusion_index_zscore
0 768915941 6.0 0.00 0.007132 0.005762 [0.008543769735872309, 0.007813074390248786, -... [0.01655297214546789, 0.018861630796218776, 0.... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.388872
1 768915941 6.0 0.15 0.002753 0.006746 [0.0234037331941625, -0.008217008895151413, -0... [0.011251730909133575, 0.024109834726767764, 0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.388872
2 768915941 6.0 0.30 -0.005194 0.009858 [0.0327278887627024, 0.030279690087803908, -0.... [0.020844536612701477, 0.014577636441001546, 0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.388872
3 768915941 6.0 0.40 0.008397 0.007399 [-0.03827911345891171, 0.011186089977715186, -... [0.019281829090979294, 0.018007816915256207, 0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.388872
4 768915941 6.0 0.50 0.008519 0.008346 [-0.009726153905977203, 0.01902948127544127, 0... [0.019741392317524198, 0.02088624239877071, 0.... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.388872
5 768915941 6.0 0.60 0.009253 0.011665 [-0.025178352688432104, 0.017183918380335665, ... [0.02817197410286694, 0.02438122885652009, 0.0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.388872
6 768915941 17.0 0.00 0.015128 0.008645 [0.019701833758226425, 0.023710864575264085, 0... [0.026784984535598043, 0.037943529096369145, 0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.382596
7 768915941 17.0 0.15 0.020182 0.006439 [0.005305779069759863, -0.04011808455654804, -... [0.016056774319860558, 0.0142993954627923, 0.0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.382596
8 768915941 17.0 0.30 0.025068 0.017557 [0.023288719736160028, 0.0010679292026893998, ... [0.024804632709051175, 0.02591368583630147, 0.... False 0.1 0.1 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.382596
9 768915941 17.0 0.40 0.004080 0.003932 [0.04928234550400494, -0.0008330831300793866, ... [0.018936587792229866, 0.01329548319540091, 0.... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.382596
10 768915941 17.0 0.50 0.004800 0.007713 [-6.631287946106102e-05, 0.013149778808444437,... [0.01788750971809229, 0.02017633629923101, 0.0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.382596
11 768915941 17.0 0.60 0.000649 0.005235 [-0.07529059462885326, -0.03986243420613151, -... [0.02074179302576191, 0.025424163652726182, 0.... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.382596
12 768915941 22.0 0.00 0.017012 0.015335 [-0.023304352518631236, -0.013316530715032115,... [0.02181733004710602, 0.026669951588005475, 0.... False 0.0 0.1 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.163405
13 768915941 22.0 0.15 0.011795 0.007032 [0.0668809276932086, 0.05743368089525116, 0.08... [0.034279002443307345, 0.03601595196870533, 0.... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.163405
14 768915941 22.0 0.30 0.005241 0.007287 [0.014895615458080288, -0.023844327179981313, ... [0.02697061278717793, 0.01479592968224692, 0.0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.163405
15 768915941 22.0 0.40 0.019665 0.009470 [0.08503395351709955, 0.06465878095537383, 0.0... [0.05179662983656048, 0.05929509584113284, 0.0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.163405
16 768915941 22.0 0.50 0.002539 0.005917 [0.034848682601038836, 0.0629781229121116, 0.0... [0.02963722610961506, 0.03315444186192063, 0.0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.163405
17 768915941 22.0 0.60 0.006282 0.010754 [-0.012169557061717944, -0.01116393997260332, ... [0.02246391896387979, 0.02999254887384419, 0.0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.163405
18 768915941 51.0 0.00 -0.000052 0.008092 [0.01792387846615069, 0.015113002067043538, -0... [0.021107180993586785, 0.016565461301937024, 0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.692242
19 768915941 51.0 0.15 0.022343 0.010008 [-0.005034375245148916, 0.02954024550785444, -... [0.02611256305091594, 0.018914335936679228, 0.... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.692242
20 768915941 51.0 0.30 -0.002876 0.006567 [-0.032476940308360035, 0.013904708795117102, ... [0.017762239125434783, 0.027265256020911063, 0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.692242
21 768915941 51.0 0.40 0.009418 0.009880 [0.004630007197667087, -0.001697099071323908, ... [0.018244619077108563, 0.010580726497757539, 0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.692242
22 768915941 51.0 0.50 0.090681 0.070838 [0.016778562173493234, 0.002042430790530056, -... [0.011339746525766343, 0.026729132031776816, 0... False 0.1 0.1 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.692242
23 768915941 51.0 0.60 0.009444 0.003112 [-0.001564995837826147, 0.003659138100209286, ... [0.015010166617593499, 0.02244451434780519, 0.... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 0.692242
24 768915941 71.0 0.00 0.015296 0.004368 [0.028637692590601633, -0.0004515634600655384,... [0.023147939152793725, 0.018627550539052535, 0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.292181
25 768915941 71.0 0.15 0.003594 0.006680 [0.01480756968264826, 0.00889621744142131, -0.... [0.01770688478551277, 0.016939820689417098, 0.... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.292181
26 768915941 71.0 0.30 0.010314 0.005691 [-0.014205599882950646, -0.0025694753607208324... [0.017251292279160096, 0.015229654100995796, 0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.292181
27 768915941 71.0 0.40 0.009000 0.005616 [0.040823229642103796, 0.025547882384470356, -... [0.014522483807209862, 0.020683032795651424, 0... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.292181
28 768915941 71.0 0.50 0.005484 0.009147 [-0.02204545731107723, 0.0020529916794289407, ... [0.01978131901824885, 0.019160141254174545, 0.... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.292181
29 768915941 71.0 0.60 0.016312 0.010698 [-0.004923068607164434, -0.01709297548014809, ... [0.023073019558709204, 0.01317118801258564, 0.... False 0.0 0.0 ... behavior_session_1 416366 2018-10-25 753787399 1 RSP superficial RSP_superficial False 1.292181
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
388230 837329471 89.0 0.00 -0.001262 0.005089 [0.008657553935310548, 0.008136579350378618, -... [0.01200225228469254, 0.015406273037784236, 0.... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 2.191180
388231 837329471 89.0 0.15 0.003781 0.006823 [-0.01887564634167585, 0.009968787185233929, -... [0.007581400302018384, 0.017640358191040185, 0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 2.191180
388232 837329471 89.0 0.30 0.001721 0.004911 [-0.0020118032436154377, 0.004445673708891552,... [0.008426214813760456, 0.015391441859457091, 0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 2.191180
388233 837329471 89.0 0.40 -0.000177 0.004604 [-0.013146219408361035, 0.007691281861574348, ... [0.0143063036539375, 0.013062337622604173, 0.0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 2.191180
388234 837329471 89.0 0.50 0.003639 0.005173 [-0.0007606738679262093, 0.012650612983352788,... [0.03179563850928509, 0.02216047264815865, 0.0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 2.191180
388235 837329471 89.0 0.60 0.003541 0.003756 [-0.004782680593786853, -0.008099911152729077,... [0.01071318813801622, 0.009175428221407265, 0.... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 2.191180
388236 837329471 103.0 0.00 0.000979 0.004423 [0.011201658101294674, -0.0007529894716821605,... [0.017756715740451262, 0.013511815775422358, 0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.398944
388237 837329471 103.0 0.15 0.001959 0.003917 [-0.004010231124120745, -0.009395679771126018,... [0.010952650214824201, 0.010494793675500137, 0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.398944
388238 837329471 103.0 0.30 -0.000124 0.005333 [-0.03691727632258842, -0.011354671661435316, ... [0.0072644767697276035, 0.007771383641320858, ... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.398944
388239 837329471 103.0 0.40 0.003915 0.003127 [-0.020417378814112663, 0.025063065508084514, ... [0.011641970951652572, 0.010869704422292117, 0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.398944
388240 837329471 103.0 0.50 -0.001855 0.003697 [1.373685634391865e-05, 0.0071148631947723275,... [0.01404108774978659, 0.016167439449943533, 0.... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.398944
388241 837329471 103.0 0.60 -0.008471 0.004500 [-0.023464538580320764, -0.021593822797931022,... [0.0116131735289468, 0.008383411038679537, 0.0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.398944
388242 837329471 110.0 0.00 0.004955 0.003622 [0.0177092177495004, 0.004387248991614883, -0.... [0.012813372153083097, 0.008779671161992799, 0... True 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.126747
388243 837329471 110.0 0.15 0.003578 0.005021 [-0.003943104740779853, 0.0015628097721903267,... [0.0154601614278482, 0.008581719684563152, 0.0... True 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.126747
388244 837329471 110.0 0.30 -0.008214 0.005613 [-0.011501562536859399, -0.004827754594730439,... [0.005796807513583372, 0.010755599971044598, 0... True 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.126747
388245 837329471 110.0 0.40 0.007582 0.007515 [-0.011822410852877486, -0.013958261122501373,... [0.012097569060528805, 0.009269049312658118, 0... True 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.126747
388246 837329471 110.0 0.50 -0.003834 0.004641 [-0.02029334233913719, -0.004207768081515526, ... [0.017406338861595146, 0.010580307504140731, 0... True 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.126747
388247 837329471 110.0 0.60 0.018967 0.007698 [0.012697343326625084, -0.015468659307720243, ... [0.017445695294942134, 0.014528397369487318, 0... True 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.126747
388248 837329471 111.0 0.00 0.017551 0.010416 [0.0009160762786264203, -0.016564767571488375,... [0.009796921155671169, 0.017951856028087687, 0... False 0.1 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 4.826353
388249 837329471 111.0 0.15 0.003892 0.003949 [0.00019568875235089316, -0.026518935426774647... [0.00912744079994605, 0.009634462053795105, 0.... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 4.826353
388250 837329471 111.0 0.30 0.000998 0.006489 [0.024695923550263538, 0.0005894354214815579, ... [0.009149923909575182, 0.012406674514217574, 0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 4.826353
388251 837329471 111.0 0.40 0.007867 0.007845 [-0.002513177247169044, 0.0036654354341307768,... [0.010974612555452594, 0.01682542021819395, 0.... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 4.826353
388252 837329471 111.0 0.50 0.004043 0.006056 [0.0025217587770150138, -0.018683994817912268,... [0.014551870773371103, 0.01302253267423027, 0.... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 4.826353
388253 837329471 111.0 0.60 0.000639 0.003260 [-0.004321510277756155, 0.012208648973014534, ... [0.01063840763710146, 0.01206497092280647, 0.0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 4.826353
388254 837329471 112.0 0.00 0.001026 0.004534 [0.022441753602935356, 0.01108823938560707, -0... [0.008579782130241208, 0.007876842878026155, 0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.715249
388255 837329471 112.0 0.15 0.003166 0.005367 [-0.017619074921099428, -0.01980333858852146, ... [0.00990550769332451, 0.015506199274303798, 0.... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.715249
388256 837329471 112.0 0.30 0.012127 0.005579 [0.004544991430047118, -0.00038114679112645245... [0.011022830094180378, 0.008577369359856691, 0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.715249
388257 837329471 112.0 0.40 -0.001974 0.008244 [-0.006291563530774398, -0.025355000353298785,... [0.011390337258662354, 0.01109736648552208, 0.... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.715249
388258 837329471 112.0 0.50 0.004852 0.006502 [0.010940395097867586, -0.015712987839863638, ... [0.013251098816950831, 0.013670836901472966, 0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.715249
388259 837329471 112.0 0.60 0.005031 0.007446 [-0.02454173765454241, -0.018539910721319032, ... [0.010387261086353695, 0.013271143793097335, 0... False 0.0 0.0 ... behavior_session_r 440299 2019-03-14 812996162 r VISpm superficial VISpm_superficial False 0.715249

388260 rows × 27 columns

In [240]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

metric = 'occlusion_index_zscore'

df = ocdf_metric.copy()
# df = df[df.oddball==True]
df = df[(df[metric]>-5)&(df[metric]<10)]
df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
df = df[df.fraction_significant_trials>0.1]
colors = [sns.color_palette()[0], sns.color_palette()[3], sns.color_palette()[2]]

fig, ax = plt.subplots(1, figsize=(5,5), sharey=True, sharex=True)
for i, area in enumerate(['VISp', 'VISpm', 'RSP']):
    ax = sns.distplot(df[(df.area==area)&(df.retrogradely_labeled==label)][metric].values, bins=30,
                  color=colors[i], ax=ax, label=area)
    ax.set_xlabel(metric)
    legend = ax.legend(fontsize='x-small', title='area', loc='upper right')
    plt.setp(legend.get_title(),fontsize='x-small')
    ax.set_ylabel('density')
#     ax.set_title(area)
In [241]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

metric = 'occlusion_index_zscore'

df = ocdf_metric.copy()
# df = df[df.oddball==True]
df = df[(df[metric]>-5)&(df[metric]<10)]
df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
df = df[df.fraction_significant_trials>0.1]
colors = [sns.color_palette()[0], sns.color_palette()[3]]

fig, ax = plt.subplots(1,3, figsize=(15,5), sharey=True, sharex=True)
for i, area in enumerate(['VISp', 'VISpm', 'RSP']):
    for c, label in enumerate(df.retrogradely_labeled.unique()):
        ax[i] = sns.distplot(df[(df.area==area)&(df.retrogradely_labeled==label)][metric].values, bins=30,
                      color=colors[c], ax=ax[i], label=label)
        ax[i].set_xlabel(metric)
        legend = ax[i].legend(fontsize='x-small', title='retrogradely labeled', loc='upper right')
        plt.setp(legend.get_title(),fontsize='x-small')
        ax[i].set_title(area)
ax[0].set_ylabel('density')
Out[241]:
Text(0, 0.5, 'density')
In [242]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

metric = 'oddball_response_index'

metric = 'occlusion_index_zscore'

df = ocdf_metric.copy()
# df = df[df.oddball==True]
df = df[(df[metric]>-5)&(df[metric]<10)]
df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
df = df[df.fraction_significant_trials>0.1]

fig, ax = plt.subplots(1,3, figsize=(15,5), sharey=True, sharex=True)
for i, area in enumerate(['VISp', 'VISpm', 'RSP']):
    for c, depth in enumerate(df.depth.unique()):
        ax[i] = sns.distplot(df[(df.area==area)&(df.depth==depth)][metric].values, bins=30,
                      color=colors[c], ax=ax[i], label=depth)
        ax[i].set_xlabel(metric)
        ax[i].legend()
        ax[i].set_title(area)
ax[0].set_ylabel('density')
Out[242]:
Text(0, 0.5, 'density')
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [231]:
def plot_occlusion_responses(ocdf, metric_df, cell_specimen_id, save_dir=None):
    images = oddball_images
    colors = sns.color_palette('Blues')
    figsize=(20,2.5)
    fig, ax = plt.subplots(1, len(images), figsize=figsize, sharey=True)
    for i,image_id in enumerate(images): 
        cell_ocdf = ocdf[(ocdf.cell_specimen_id==cell_specimen_id)&(ocdf.image_id==image_id)]
        for o, fraction_occlusion in enumerate(np.sort(ocdf.fraction_occlusion.unique())):
            occ_df = cell_ocdf[cell_ocdf.fraction_occlusion==fraction_occlusion]
            ax[i].plot(occ_df.mean_trace.values[0], color=colors[o], label=fraction_occlusion)
        xticks, xticklabels = sf.get_xticks_xticklabels(cell_ocdf.mean_trace.values[0], 31.)
        ax[i].set_xticks(xticks)
        ax[i].set_xticklabels(xticklabels)
        ax[i].set_xlabel('time (s)')
        metric_value = metric_df[(metric_df.cell_specimen_id==cell_specimen_id)&(metric_df.image_id==image_id)].occlusion_index_zscore.values[0]
        ax[i].set_title(str(np.round(metric_value,2))+', '+str(np.round(cell_ocdf.fraction_significant_trials.values[0], 2)),fontsize=14)
                                        
    ax[0].set_ylabel('dF/F')
    legend = ax[i].legend(fontsize='x-small', title='fraction occlusion', bbox_to_anchor=(1,1))
    plt.setp(legend.get_title(),fontsize='x-small')
    fig.tight_layout()
    fig.subplots_adjust(bottom=0.25)
    fig.subplots_adjust(right=0.8)
    if save_dir: 
        sf.save_figure(fig, figsize, save_dir, 'occlusion_responses', str(int(cell_specimen_id)))
        plt.close()
In [232]:
plot_occlusion_responses(ocdf, metric_df, cell_specimen_id, save_dir=None)
In [235]:
for cell_specimen_id in ocdf.cell_specimen_id.unique():
    plot_occlusion_responses(ocdf, metric_df, cell_specimen_id, save_dir=save_dir)
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-235-5e30302afb18> in <module>
      1 for cell_specimen_id in ocdf.cell_specimen_id.unique():
----> 2     plot_occlusion_responses(ocdf, metric_df, cell_specimen_id, save_dir=save_dir)

<ipython-input-231-d6372c8ce86f> in plot_occlusion_responses(ocdf, metric_df, cell_specimen_id, save_dir)
     19     legend = ax[i].legend(fontsize='x-small', title='fraction occlusion', bbox_to_anchor=(1,1))
     20     plt.setp(legend.get_title(),fontsize='x-small')
---> 21     fig.tight_layout()
     22     fig.subplots_adjust(bottom=0.25)
     23     fig.subplots_adjust(right=0.8)

~\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\matplotlib\figure.py in tight_layout(self, renderer, pad, h_pad, w_pad, rect)
   2473         kwargs = get_tight_layout_figure(
   2474             self, self.axes, subplotspec_list, renderer,
-> 2475             pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
   2476         if kwargs:
   2477             self.subplots_adjust(**kwargs)

~\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\matplotlib\tight_layout.py in get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer, pad, h_pad, w_pad, rect)
    360                                      subplot_list=subplot_list,
    361                                      ax_bbox_list=ax_bbox_list,
--> 362                                      pad=pad, h_pad=h_pad, w_pad=w_pad)
    363 
    364     # kwargs can be none if tight_layout fails...

~\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\matplotlib\tight_layout.py in auto_adjust_subplotpars(fig, renderer, nrows_ncols, num1num2_list, subplot_list, ax_bbox_list, pad, h_pad, w_pad, rect)
    109             continue
    110 
--> 111         tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
    112                                 if ax.get_visible()])
    113         tight_bbox = TransformedBbox(tight_bbox_raw,

~\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\matplotlib\tight_layout.py in <listcomp>(.0)
    110 
    111         tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
--> 112                                 if ax.get_visible()])
    113         tight_bbox = TransformedBbox(tight_bbox_raw,
    114                                      fig.transFigure.inverted())

~\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\matplotlib\axes\_base.py in get_tightbbox(self, renderer, call_axes_locator, bbox_extra_artists)
   4372 
   4373         for a in bbox_artists:
-> 4374             bbox = a.get_tightbbox(renderer)
   4375             if (bbox is not None
   4376                     and 0 < bbox.width < np.inf

~\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\matplotlib\artist.py in get_tightbbox(self, renderer)
    265             The enclosing bounding box (in figure pixel co-ordinates).
    266         """
--> 267         bbox = self.get_window_extent(renderer)
    268         if self.get_clip_on():
    269             clip_box = self.get_clip_box()

~\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\matplotlib\patches.py in get_window_extent(self, renderer)
    587 
    588     def get_window_extent(self, renderer=None):
--> 589         return self.get_path().get_extents(self.get_transform())
    590 
    591     def _convert_xy_units(self, xy):

~\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\matplotlib\path.py in get_extents(self, transform)
    507         path = self
    508         if transform is not None:
--> 509             transform = transform.frozen()
    510             if not transform.is_affine:
    511                 path = self.transformed(transform)

~\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\matplotlib\transforms.py in frozen(self)
   2323         frozen = composite_transform_factory(self._a.frozen(), self._b.frozen())
   2324         if not isinstance(frozen, CompositeGenericTransform):
-> 2325             return frozen.frozen()
   2326         return frozen
   2327 

~\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\matplotlib\transforms.py in frozen(self)
   1748     def frozen(self):
   1749         # docstring inherited
-> 1750         return Affine2D(self.get_matrix().copy())
   1751 
   1752     @property

~\Anaconda3\envs\visual_behavior_sdk\lib\site-packages\matplotlib\transforms.py in get_matrix(self)
   2462             self._mtx = np.dot(
   2463                 self._b.get_matrix(),
-> 2464                 self._a.get_matrix())
   2465             self._inverted = None
   2466             self._invalid = 0

<__array_function__ internals> in dot(*args, **kwargs)

KeyboardInterrupt: 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [116]:
odf = odf.reset_index()

occlusion_index = []
for index in odf.index.values: 
    row_df = odf.iloc[index]
    cell_specimen_id = row_df.cell_specimen_id
    image_id = row_df.image_id
    occlusion_mean_response = row_df.mean_response
    tcdf = tdf[(tdf.image_id==image_id)&(tdf.cell_specimen_id==cell_specimen_id)&(tdf.second_in_sequence==True)]
    transitions_mean_response = tcdf.mean_response.values[0]
    occlusion_response_index = ((oddball_mean_response-transitions_mean_response)/(oddball_mean_response+transitions_mean_response))
    occlusion_index.append(occlusion_response_index)
odf['occlusion_response_index'] = occlusion_index
Out[116]:
Index(['index', 'cell_specimen_id', 'image_id', 'oddball', 'mean_response_x',
       'sem_response', 'mean_trace', 'sem_trace', 'pref_stim',
       'fraction_significant_trials', 'fraction_responsive_trials',
       'experiment_id', 'experiment_container_id', 'targeted_structure',
       'imaging_depth', 'cre_line', 'reporter_line', 'full_genotype',
       'session_type', 'donor_id', 'experiment_date', 'specimen_id',
       'session_num', 'area', 'depth', 'location', 'retrogradely_labeled',
       'oddball_response_index', 'mean_response_y'],
      dtype='object')
In [ ]:
 
In [ ]:
ocdf = ocdf.reset_index()

oddball_index = []
for index in ocdf.index.values: 
    row_df = ocdf.iloc[index]
    cell_specimen_id = row_df.cell_specimen_id
    image_id = row_df.image_id
    oddball_mean_response = row_df.mean_response
    tcdf = tdf[(tdf.image_id==image_id)&(tdf.cell_specimen_id==cell_specimen_id)&(tdf.second_in_sequence==True)]
    transitions_mean_response = tcdf.mean_response.values[0]
    oddball_response_index = ((oddball_mean_response-transitions_mean_response)/(oddball_mean_response+transitions_mean_response))
    oddball_index.append(oddball_response_index)
odf['occlusion_response_index'] = oddball_index
In [ ]:
 
In [ ]:
 
In [ ]:
df = ocdf.copy()

df_shallowVISp = df[(df['targeted_structure']=='VISp')&(df['imaging_depth']<=300)]

df_focus = df_shallowVISp
del df_shallowVISp

responsive_cells = df_focus.cell_specimen_id.unique()

for ii in range(0,len(responsive_cells)):
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]])<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if not 0 in list(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion']):
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion'].unique())<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]

ori_bycell=occlusion_response_index_bycell(df_focus)
ori_shallowVISp=ori_bycell.dropna()


# In[]: 
df_deepVISp = df[(df['targeted_structure']=='VISp')&(df['imaging_depth']>300)]

df_focus = df_deepVISp
del df_deepVISp

responsive_cells = df_focus.cell_specimen_id.unique()

for ii in range(0,len(responsive_cells)):
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]])<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if not 0 in list(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion']):
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion'].unique())<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]

ori_bycell=occlusion_response_index_bycell(df_focus)
ori_deepVISp=ori_bycell.dropna()


# In[]:      

df_shallowVISpm = df[(df['targeted_structure']=='VISpm')&(df['imaging_depth']<=300)]

df_focus = df_shallowVISpm
del df_shallowVISpm

responsive_cells = df_focus.cell_specimen_id.unique()

for ii in range(0,len(responsive_cells)):
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]])<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if not 0 in list(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion']):
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion'].unique())<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]

ori_bycell=occlusion_response_index_bycell(df_focus)
ori_shallowVISpm=ori_bycell.dropna()


# In[]: 
df_deepVISpm = df[(df['targeted_structure']=='VISpm')&(df['imaging_depth']>300)]

df_focus = df_deepVISpm
del df_deepVISpm

responsive_cells = df_focus.cell_specimen_id.unique()

for ii in range(0,len(responsive_cells)):
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]])<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if not 0 in list(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion']):
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion'].unique())<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]

ori_bycell=occlusion_response_index_bycell(df_focus)
ori_deepVISpm=ori_bycell.dropna()


# In[]:      
#cache_dir = r'\\allen\programs\braintv\workgroups\nc-ophys\opc\opc_analysis'
#cache_dir = r"C:\Users\hannahc\Desktop\opc_data"

df_shallowRSP = df[(df['targeted_structure']=='RSP')&(df['imaging_depth']<=300)]

df_focus = df_shallowRSP
del df_shallowRSP

responsive_cells = df_focus.cell_specimen_id.unique()

for ii in range(0,len(responsive_cells)):
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]])<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if not 0 in list(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion']):
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion'].unique())<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]

ori_bycell=occlusion_response_index_bycell(df_focus)
ori_shallowRSP=ori_bycell.dropna()


# In[]: 
df_deepRSP = df[(df['targeted_structure']=='VISp')&(df['imaging_depth']>300)]

df_focus = df_deepRSP
del df_deepRSP

responsive_cells = df_focus.cell_specimen_id.unique()

for ii in range(0,len(responsive_cells)):
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]])<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if not 0 in list(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion']):
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]
    if len(df_focus[df_focus['cell_specimen_id']==responsive_cells[ii]]['fraction_occlusion'].unique())<3:
        df_focus = df_focus[df_focus['cell_specimen_id']!=responsive_cells[ii]]

ori_bycell=occlusion_response_index_bycell(df_focus)
ori_deepRSP=ori_bycell.dropna()


# In[]: 

areas = [['shallowVISp']*len(ori_shallowVISp)+ ['deepVISp']*len(ori_deepVISp)
+['shallowVISpm']*len(ori_shallowVISpm)+ ['deepVISpm']*len(ori_deepVISpm)+['shallowRSP']*len(ori_shallowRSP)+ ['deepRSP']*len(ori_deepRSP)]
areas =np.array(areas[0])
all_ori = ori_shallowVISp.append(ori_deepVISp)
all_ori= all_ori.append(ori_shallowVISpm)
all_ori= all_ori.append(ori_deepVISpm)
all_ori= all_ori.append(ori_shallowRSP)
all_ori= all_ori.append(ori_deepRSP)
df_ori = pd.DataFrame({'ORI':all_ori,'area':areas} )

# In[]: 
ori_thresh = 1
df_occlusion_pref = df_ori[df_ori.ORI>=1]
n_occlusion_pref = df_occlusion_pref.groupby('area').count()
df_occlusion_nonpref = df_ori[df_ori.ORI<1]
n_occlusion_nonpref = df_occlusion_nonpref.groupby('area').count()

n_occlusion_pref = n_occlusion_pref.rename(columns={'ORI': 'occlusion preferred'})
n_occlusion_nonpref = n_occlusion_nonpref.rename(columns={'ORI': 'occlusion not preferred'})
newdf =pd.concat([n_occlusion_pref,n_occlusion_nonpref], axis=1)
In [ ]:
# In[]:
arealist = list(newdf.index.unique())
fig,ax = plt.subplots(figsize=(9,4))
ax.boxplot([df_ori.ORI[df_ori.area==arealist[0]].values,df_ori.ORI[df_ori.area==arealist[1]].values,
            df_ori.ORI[df_ori.area==arealist[2]].values,df_ori.ORI[df_ori.area==arealist[3]].values,
            df_ori.ORI[df_ori.area==arealist[4]].values,df_ori.ORI[df_ori.area==arealist[5]].values])
ax.set_xticks(np.arange(1,7,1))
ax.set_xticklabels(tuple(arealist),fontsize = 12)
ax.set_ylim([0,50])
ax.set_ylabel('Occlusion Response Index',fontsize = 14)
plt.show()

# In[]: 

import seaborn as sns        
%matplotlib inline

#ax = sns.scatterplot(x="areas", y="ORI", hue="areas",data=df_ori)

fig,ax = plt.subplots()
arealist = list(newdf.index.unique())
colors = ["red"]
jj = 0
for i in arange(1,7,1):
    y = df_ori.ORI[df_ori.area==arealist[jj]]
    # Add some random "jitter" to the x-axis
    x = np.random.normal(i, 0.03, size=len(y))
    ax.scatter(x, y, c=colors[0], alpha=0.5)
    jj = jj+1

ax.boxplot([df_ori.ORI[df_ori.area==arealist[0]].values,df_ori.ORI[df_ori.area==arealist[1]].values,
            df_ori.ORI[df_ori.area==arealist[2]].values,df_ori.ORI[df_ori.area==arealist[3]].values,
            df_ori.ORI[df_ori.area==arealist[4]].values,df_ori.ORI[df_ori.area==arealist[5]].values])
ax.set_xticks(arealist)
ax.set_ylabel('Occlusion Response Index')
plt.show()
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

pick a cell and plot stuff

In [89]:
save_dir = os.path.join(cache_dir, 'multi_session_summary_figures')
In [25]:
cell_specimen_id = cell_specimen_ids[10]
In [86]:
cell_specimen_id = responsive_cells[1]
In [90]:
figsize = (5,4)
fig, ax = plt.subplots(figsize=figsize)
for image_id in images[::-1]: 
    cdf = odf[(odf.image_id==image_id)&(odf.cell_specimen_id==cell_specimen_id)]
    mean_trace = cdf.mean_trace.values[0]
    if image_id in sequence_images: 
        ind = np.where(sequence_images==image_id)[0][0]
        color = sequence_colors[ind]
    else: 
        ind = np.where(oddball_images==image_id)[0][0]
        color = oddball_colors[ind]
    ax.plot(mean_trace, color=color)
    xticks, xticklabels = sf.get_xticks_xticklabels(mean_trace, 31., interval_sec=1)
    ax.set_xticks(xticks)
    ax.set_xticklabels([int(x) for x in xticklabels])
    ax.set_ylabel('dF/F')
    ax.set_xlabel('time (sec)')
    ax.set_title(cell_specimen_id)
In [93]:
for cell_specimen_id in cell_specimen_ids:
    figsize = (5,4)
    fig, ax = plt.subplots(figsize=figsize)
    for image_id in images[::-1]: 
        cdf = odf[(odf.image_id==image_id)&(odf.cell_specimen_id==cell_specimen_id)]
        mean_trace = cdf.mean_trace.values[0]
        if image_id in sequence_images: 
            ind = np.where(sequence_images==image_id)[0][0]
            color = sequence_colors[ind]
        else: 
            ind = np.where(oddball_images==image_id)[0][0]
            color = oddball_colors[ind]
        ax.plot(mean_trace, color=color)
        xticks, xticklabels = sf.get_xticks_xticklabels(mean_trace, 31., interval_sec=1)
        ax.set_xticks(xticks)
        ax.set_xticklabels([int(x) for x in xticklabels])
        ax.set_ylabel('dF/F')
        ax.set_xlabel('time (sec)')
        ax.set_title(cell_specimen_id)
    sf.save_figure(fig, figsize, save_dir, 'oddball_mean_trace', str(cell_specimen_id))
    plt.close()
In [94]:
figsize = (14,5)
fig, ax = plt.subplots(2,7,figsize=figsize, sharex=True, sharey=True)
ax = ax.ravel()
for i,image_id in enumerate(images): 
    cdf = odf[(odf.image_id==image_id)&(odf.cell_specimen_id==cell_specimen_id)]
    mean_trace = cdf.mean_trace.values[0]
    ax[i].plot(mean_trace, color='green', label='oddball block')
    tcdf = tdf[(tdf.image_id==image_id)&(tdf.cell_specimen_id==cell_specimen_id)]
    mean_trace = tcdf.mean_trace.values[0]
    ax[i].plot(mean_trace, color='magenta', label='transition control block')
    xticks, xticklabels = sf.get_xticks_xticklabels(mean_trace, 31., interval_sec=1)
    ax[i].set_xticks(xticks)
    ax[i].set_xticklabels(xticklabels)
    ax[i].set_title(str(int(image_id)))
ax[0].set_ylabel('dF/F')
ax[7].set_ylabel('dF/F')
for i in range(7,14):
    ax[i].set_xlabel('time (sec)')
fig.tight_layout()
plt.suptitle(str(cell_specimen_id), x=0.525, y=.97, fontsize=18)
plt.subplots_adjust(top=0.85)
In [95]:
for cell_specimen_id in cell_specimen_ids: 
    figsize = (14,5)
    fig, ax = plt.subplots(2,7,figsize=figsize, sharex=True, sharey=True)
    ax = ax.ravel()
    for i,image_id in enumerate(images): 
        cdf = odf[(odf.image_id==image_id)&(odf.cell_specimen_id==cell_specimen_id)]
        mean_trace = cdf.mean_trace.values[0]
        ax[i].plot(mean_trace, color='green', label='oddball block')
        tcdf = tdf[(tdf.image_id==image_id)&(tdf.cell_specimen_id==cell_specimen_id)]
        mean_trace = tcdf.mean_trace.values[0]
        ax[i].plot(mean_trace, color='magenta', label='transition control block')
        xticks, xticklabels = sf.get_xticks_xticklabels(mean_trace, 31., interval_sec=1)
        ax[i].set_xticks(xticks)
        ax[i].set_xticklabels(xticklabels)
        ax[i].set_title(str(int(image_id)))
    ax[0].set_ylabel('dF/F')
    ax[7].set_ylabel('dF/F')
    for i in range(7,14):
        ax[i].set_xlabel('time (sec)')
    fig.tight_layout()
    plt.suptitle(str(cell_specimen_id), x=0.525, y=.97, fontsize=18)
    plt.subplots_adjust(top=0.85)
    sf.save_figure(fig, figsize, save_dir, 'oddball_transition_mean_trace', str(cell_specimen_id))
    plt.close()
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-95-4bde3d1260b8> in <module>
      1 for cell_specimen_id in cell_specimen_ids:
      2     figsize = (14,5)
----> 3     fig, ax = plt.subplots(2,7,figsize=figsize, sharex=True, sharey=True)
      4     ax = ax.ravel()
      5     for i,image_id in enumerate(images):

C:\Anaconda\envs\visual_behavior_sdk\lib\site-packages\matplotlib\pyplot.py in subplots(nrows, ncols, sharex, sharey, squeeze, subplot_kw, gridspec_kw, **fig_kw)
   1218     axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,
   1219                        squeeze=squeeze, subplot_kw=subplot_kw,
-> 1220                        gridspec_kw=gridspec_kw)
   1221     return fig, axs
   1222 

C:\Anaconda\envs\visual_behavior_sdk\lib\site-packages\matplotlib\figure.py in subplots(self, nrows, ncols, sharex, sharey, squeeze, subplot_kw, gridspec_kw)
   1527                 subplot_kw["sharex"] = shared_with[sharex]
   1528                 subplot_kw["sharey"] = shared_with[sharey]
-> 1529                 axarr[row, col] = self.add_subplot(gs[row, col], **subplot_kw)
   1530 
   1531         # turn off redundant tick labeling

C:\Anaconda\envs\visual_behavior_sdk\lib\site-packages\matplotlib\figure.py in add_subplot(self, *args, **kwargs)
   1365                     self._axstack.remove(ax)
   1366 
-> 1367             a = subplot_class_factory(projection_class)(self, *args, **kwargs)
   1368         self._axstack.add(key, a)
   1369         self.sca(a)

C:\Anaconda\envs\visual_behavior_sdk\lib\site-packages\matplotlib\axes\_subplots.py in __init__(self, fig, *args, **kwargs)
     68 
     69         # _axes_class is set in the subplot_class_factory
---> 70         self._axes_class.__init__(self, fig, self.figbox, **kwargs)
     71         # add a layout box to this, for both the full axis, and the poss
     72         # of the axis.  We need both because the axes may become smaller

C:\Anaconda\envs\visual_behavior_sdk\lib\site-packages\matplotlib\axes\_base.py in __init__(self, fig, rect, facecolor, frameon, sharex, sharey, label, xscale, yscale, **kwargs)
    527             labelright=(rcParams['ytick.labelright'] and
    528                         rcParams['ytick.minor.right']),
--> 529             which='minor')
    530 
    531         self.tick_params(

C:\Anaconda\envs\visual_behavior_sdk\lib\site-packages\matplotlib\axes\_base.py in tick_params(self, axis, **kwargs)
   2994             ykw.pop('labeltop', None)
   2995             ykw.pop('labelbottom', None)
-> 2996             self.yaxis.set_tick_params(**ykw)
   2997 
   2998     def set_axis_off(self):

C:\Anaconda\envs\visual_behavior_sdk\lib\site-packages\matplotlib\axis.py in set_tick_params(self, which, reset, **kw)
    864             if which == 'minor' or which == 'both':
    865                 for tick in self.minorTicks:
--> 866                     tick._apply_params(**self._minor_tick_kw)
    867             if 'labelcolor' in kwtrans:
    868                 self.offsetText.set_color(kwtrans['labelcolor'])

C:\Anaconda\envs\visual_behavior_sdk\lib\site-packages\matplotlib\axis.py in _apply_params(self, **kw)
    376         label_kw = {k[5:]: v for k, v in kw.items()
    377                     if k in ['labelsize', 'labelcolor']}
--> 378         self.label1.set(**label_kw)
    379         self.label2.set(**label_kw)
    380         for k, v in label_kw.items():

C:\Anaconda\envs\visual_behavior_sdk\lib\site-packages\matplotlib\artist.py in set(self, **kwargs)
   1011         props = OrderedDict(
   1012             sorted(kwargs.items(), reverse=True,
-> 1013                    key=lambda x: (self._prop_order.get(x[0], 0), x[0])))
   1014 
   1015         return self.update(props)

KeyboardInterrupt: 
In [ ]:
 
In [76]:
figsize = (6,4)
fig, ax = plt.subplots(figsize=figsize)
for i,image_id in enumerate(images): 
    cdf = odf[(odf.image_id==image_id)&(odf.cell_specimen_id==cell_specimen_id)]
    mean_response = cdf.mean_response.values[0]
    ax.plot(i, mean_response, 'o', color='green')
    tcdf = tdf[(tdf.image_id==image_id)&(tdf.cell_specimen_id==cell_specimen_id)]
    mean_response = tcdf.mean_response.values[0]
    ax.plot(i, mean_response, 'o' ,color='magenta')
    
cdf = odf[(odf.image_id==image_id)&(odf.cell_specimen_id==cell_specimen_id)]
mean_response = cdf.mean_response.values[0]
ax.plot(i, mean_response, 'o', color='green', label='sequence')
tcdf = tdf[(tdf.image_id==image_id)&(tdf.cell_specimen_id==cell_specimen_id)]
mean_response = tcdf.mean_response.values[0]
ax.plot(i, mean_response, 'o' ,color='magenta', label='transition control')

ax.set_title(str(int(cell_specimen_id)))
ax.set_ylabel('dF/F')
ax.set_xticks(np.arange(0,len(images)))
ax.set_xticklabels([int(image) for image in images], rotation=90)
ax.set_xlabel('image ID')
ax.legend(fontsize='x-small')
fig.tight_layout()
In [403]:
for cell_specimen_id in cell_specimen_ids: 
    figsize = (6,4)
    fig, ax = plt.subplots(figsize=figsize)
    for i,image_id in enumerate(images): 
        cdf = odf[(odf.image_id==image_id)&(odf.cell_specimen_id==cell_specimen_id)]
        mean_response = cdf.mean_response.values[0]
        ax.plot(i, mean_response, 'o', color='green')
        tcdf = tdf[(tdf.image_id==image_id)&(tdf.cell_specimen_id==cell_specimen_id)]
        mean_response = tcdf.mean_response.values[0]
        ax.plot(i, mean_response, 'o' ,color='magenta')

    cdf = odf[(odf.image_id==image_id)&(odf.cell_specimen_id==cell_specimen_id)]
    mean_response = cdf.mean_response.values[0]
    ax.plot(i, mean_response, 'o', color='green', label='sequence')
    tcdf = tdf[(tdf.image_id==image_id)&(tdf.cell_specimen_id==cell_specimen_id)]
    mean_response = tcdf.mean_response.values[0]
    ax.plot(i, mean_response, 'o' ,color='magenta', label='transition control')

    ax.set_title(str(int(cell_specimen_id)))
    ax.set_ylabel('dF/F')
    ax.set_xticks(np.arange(0,len(images)))
    ax.set_xticklabels([int(image) for image in images], rotation=90)
    ax.set_xlabel('image ID')
    ax.legend(fontsize='x-small')
    fig.tight_layout()
    sf.save_figure(fig, figsize, save_dir, 'oddball_transition_mean_response', str(cell_specimen_id))
    plt.close()
In [ ]:
 

oddball response metric

In [97]:
analysis.get_response_df_dict()
o = analysis.response_df_dict['oddball']
loading response dataframe for randomized_control_pre
loading response dataframe for oddball
merging oddball response with stim block
loading oddball block
loading response dataframe for transition_control
loading response dataframe for occlusion
loading response dataframe for natural_movie_one
loading response dataframe for randomized_control_post
In [101]:
t = analysis.response_df_dict['transition_control']
In [100]:
tmp = o[o.cell_index==1]
for image in images: 
    print(image, len(tmp[tmp.image_id==image]))
68.0 2000
78.0 2000
13.0 2000
26.0 1900
6.0 10
17.0 10
22.0 10
51.0 10
71.0 10
89.0 10
103.0 10
110.0 10
111.0 10
112.0 10
In [102]:
tmp = t[t.cell_index==1]
for image in images: 
    print(image, len(tmp[tmp.image_id==image]))
68.0 360
78.0 60
13.0 360
26.0 60
6.0 60
17.0 60
22.0 60
51.0 60
71.0 60
89.0 60
103.0 60
110.0 60
111.0 60
112.0 60
In [182]:
odf = oddball_df.copy()
tdf = transitions_df.copy()
In [183]:
odf = odf.reset_index()
odf = odf.drop(columns=['index'])
In [186]:
odf = odf.reset_index()

oddball_index = []
for index in odf.index.values: 
    row_df = odf.iloc[index]
    cell_specimen_id = row_df.cell_specimen_id
    image_id = row_df.image_id
    oddball_mean_response = row_df.mean_response
    tcdf = tdf[(tdf.image_id==image_id)&(tdf.cell_specimen_id==cell_specimen_id)]
    transitions_mean_response = tcdf.mean_response.values[0]
    oddball_response_index = ((oddball_mean_response-transitions_mean_response)/(oddball_mean_response+transitions_mean_response))
    oddball_index.append(oddball_response_index)
odf['oddball_response_index'] = oddball_index


odf['area'] = odf.targeted_structure.values
odf['depth'] = ['deep' if depth > 250 else 'superficial' for depth in odf.imaging_depth.values]
odf['location'] = None
for row in odf.index: 
    odf.loc[row, 'location'] = odf.iloc[row].area+'_'+odf.iloc[row].depth
    
    
tdf['area'] = tdf.targeted_structure.values
tdf['depth'] = ['deep' if depth > 250 else 'superficial' for depth in tdf.imaging_depth.values]
tdf['location'] = None
for row in tdf.index: 
    tdf.loc[row, 'location'] = tdf.iloc[row].area+'_'+tdf.iloc[row].depth
    
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = odf.copy()
df = df[df.oddball==True]
# df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
# df = df[df.fraction_significant_trials>0.0]

fig, ax = plt.subplots()
ax = sns.pointplot(data=df, x='location', y='oddball_response_index', order=locations, ax=ax)
ax.set_xticklabels(locations, rotation=90)
ax.set_xlabel('')
In [189]:
 
In [370]:
 
In [192]:
 
In [ ]:
 
In [380]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = odf.copy()
df = df[df.oddball==True]
# df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
# df = df[df.fraction_significant_trials>0.0]

fig, ax = plt.subplots()
ax = sns.pointplot(data=df, x='location', y='oddball_response_index', order=locations, ax=ax)
ax.set_xticklabels(locations, rotation=90)
ax.set_xlabel('')
Out[380]:
Text(0.5, 0, '')
In [399]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

metric = 'oddball_response_index'

df = odf.copy()
df = df[df.oddball==True]
df = df[(df[metric]>-2)&(df[metric]<2)]
# df = df[df.pref_stim==True]
# df = df[df.mean_response>0.05]
# df = df[df.fraction_significant_trials>0.0]

fig, ax = plt.subplots()
for c, area in enumerate(['VISp', 'VISpm', 'RSP']):
    ax = sns.distplot(df[df.area==area][metric].values, bins=30,
                      color=sns.color_palette()[c], ax=ax, label=area)
ax.legend()
ax.set_xlabel(metric)
Out[399]:
Text(0.5, 0, 'oddball_response_index')
In [222]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = odf[(odf.oddball_response_index<2)&(odf.oddball_response_index>-2)].copy()
df = df[df.oddball==True]
# df = df[df.fraction_significant_trials>0.2]
df = df[df.mean_response>0.05]
fig, ax = plt.subplots()
ax = sns.swarmplot(data=df, x='location', y='oddball_response_index', order=locations, ax=ax)
ax.set_xticklabels(locations, rotation=90)
ax.set_xlabel('')
Out[222]:
Text(0.5, 0, '')

habituated vs novel

In [ ]:
# # figsize = (6,4)
# # fig, ax = plt.subplots(figsize=figsize)

# oddball_index = []
# for index in odf.index.values: 
#     row_df = odf.iloc[index]
#     cell_specimen_id = row_df.cell_specimen_id
#     image_id = row_df.image_id
#     oddball_mean_response = row_df.mean_response
#     tcdf = tdf[(tdf.image_id==image_id)&(tdf.cell_specimen_id==cell_specimen_id)]
#     habituated_mean_response = tcdf.mean_response.mean()
#     oddball_response_index = ((oddball_mean_response-transitions_mean_response)/(oddball_mean_response+transitions_mean_response))
#     oddball_index.append(oddball_response_index)
# odf['oddball_response_index'] = oddball_index
In [ ]:
 
In [ ]:
 

mean response across experiments

In [ ]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']
In [104]:
experiment_ids = odf.experiment_id.unique()
experiment_id = experiment_ids[4]
In [338]:
len(experiment_ids)/8
Out[338]:
4.25
In [ ]:
 
In [341]:
figsize=(20,15)
fig, ax = plt.subplots(5,7,figsize=figsize, sharex=True, sharey=True)
ax = ax.ravel()
i=0
for area in odf.targeted_structure.unique()[::-1]:
    tmp = odf[odf.targeted_structure==area]
    for depth in np.sort(tmp.imaging_depth.unique()):
        tmp2 = tmp[tmp.imaging_depth==depth]
        for experiment_id in tmp2.experiment_id.unique(): 
            edf = odf[odf.experiment_id==experiment_id]   
            sequence = edf[edf.image_id==images[3]].mean_trace.mean()
            oddballs = edf[edf.oddball==True].mean_trace.mean()

            ax[i].plot(sequence, color='blue', label='sequence')
            ax[i].plot(oddballs, color='red', label='oddballs')

            xticks, xticklabels = sf.get_xticks_xticklabels(sequence, 31., interval_sec=1)
            ax[i].set_xticks(xticks)
            ax[i].set_xticklabels([int(x) for x in xticklabels])
            ax[i].set_ylabel('dF/F')
            ax[i].set_xlabel('time (sec)')
            ax[i].set_title(edf.targeted_structure.values[0]+', '+str(edf.imaging_depth.values[0]))
            i+=1
fig.tight_layout()
In [401]:
figsize=(20,15)
fig, ax = plt.subplots(5,7,figsize=figsize, sharex=True, sharey=True)
ax = ax.ravel()
i=0
for area in odf.targeted_structure.unique()[::-1]:
    tmp = odf[odf.targeted_structure==area]
    for depth in np.sort(tmp.imaging_depth.unique()):
        tmp2 = tmp[tmp.imaging_depth==depth]
        for experiment_id in tmp2.experiment_id.unique(): 
            dataset = OpenScopePredictiveCodingDataset(experiment_id, cache_dir)

            ax[i].imshow(dataset.max_projection, cmap='gray', vmax=np.amax(dataset.max_projection)/2)
            ax[i].axis('off')
            ax[i].set_title(dataset.metadata.targeted_structure.values[0]+', '+str(dataset.metadata.imaging_depth.values[0]))
            i+=1
fig.tight_layout()
In [365]:
figsize=(25,15)
fig, ax = plt.subplots(5,7,figsize=figsize, sharex=True, sharey=True)
ax = ax.ravel()
i=0
for area in odf.targeted_structure.unique()[::-1]:
    tmp = odf[odf.targeted_structure==area]
    for depth in np.sort(tmp.imaging_depth.unique()):
        tmp2 = tmp[tmp.imaging_depth==depth]
        for experiment_id in tmp2.experiment_id.unique(): 
            dataset = OpenScopePredictiveCodingDataset(experiment_id, cache_dir)
            ax[i] = esf.plot_traces_heatmap(dataset.dff_traces, ax=ax[i], save_dir=None)
            ax[i].axis('off')
            ax[i].set_title(tmp2.targeted_structure.values[0]+', '+str(tmp2.imaging_depth.values[0]))
            i+=1
fig.tight_layout()
In [352]:
def plot_mean_image_response_heatmap(mean_df, image_ids, colorbar=True, title=None, ax=None, save_dir=None):
    df = mean_df.copy()
    images = np.sort(df.image_id.unique())
    images = image_ids
    cell_list = []
    for image in images:
        tmp = df[(df.image_id == image) & (df.pref_stim == True)]
        order = np.argsort(tmp.mean_response.values)[::-1]
        cell_ids = list(tmp.cell_specimen_id.values[order])
        cell_list = cell_list + cell_ids

    response_matrix = np.empty((len(cell_list), len(images)))
    for i, cell_specimen_id in enumerate(cell_list):
        responses = []
        for image in images:
            response = df[(df.cell_specimen_id == cell_specimen_id) & (df.image_id == image)].mean_response.values[0]
            responses.append(response)
        response_matrix[i, :] = np.asarray(responses)

    if ax is None:
        figsize = (5, 8)
        fig, ax = plt.subplots(figsize=figsize)
    ax = sns.heatmap(response_matrix, cmap='magma', linewidths=0, linecolor='white', square=False,
                     vmin=0, vmax=0.3, robust=True, cbar=colorbar, 
                     cbar_kws={"drawedges": False, "shrink": 1, "label": "mean dF/F"}, ax=ax)

    if title is None:
        title = 'mean response by image'
    ax.set_title(title, va='bottom', ha='center')
    ax.set_xticks(np.arange(0,len(images),1))
    ax.set_xticklabels([int(image) for image in images], rotation=90)
    ax.set_ylabel('cells')
    interval = 10
    ax.set_yticks(np.arange(0, response_matrix.shape[0], interval))
    ax.set_yticklabels(np.arange(0, response_matrix.shape[0], interval))
    if save_dir:
        fig.tight_layout()
        sf.save_figure(fig, figsize, save_dir, 'mean_image_response_heatmap', str(experiment_id)+'_'+title)

    return ax
In [123]:
image_ids = analysis.get_image_ids()
for experiment_id in experiment_ids: 
    edf = odf[odf.experiment_id==experiment_id]
    title = edf.targeted_structure.values[0]+', '+str(edf.imaging_depth.values[0])
    plot_mean_image_response_heatmap(edf, image_ids, title=title, ax=None, save_dir=save_dir)
C:\Anaconda\envs\visual_behavior_sdk\lib\site-packages\matplotlib\pyplot.py:514: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  max_open_warning, RuntimeWarning)
In [353]:
image_ids = analysis.get_image_ids()

figsize=(20,15)
fig, ax = plt.subplots(5,7,figsize=figsize, sharex=False, sharey=False)
ax = ax.ravel()
i=0
for area in odf.targeted_structure.unique()[::-1]:
    tmp = odf[odf.targeted_structure==area]
    for depth in np.sort(tmp.imaging_depth.unique()):
        tmp2 = tmp[tmp.imaging_depth==depth]
        for experiment_id in tmp2.experiment_id.unique(): 
            edf = odf[odf.experiment_id==experiment_id]
            title = edf.targeted_structure.values[0]+', '+str(edf.imaging_depth.values[0])
            ax[i] = plot_mean_image_response_heatmap(edf, image_ids, colorbar=False, title=title, ax=ax[i], save_dir=None)
            ax[i].set_xlabel('')
            ax[i].set_xticklabels('')
            ax[i].set_yticklabels('')
            ax[i].set_ylabel('')
            i+=1
fig.tight_layout()
In [402]:
image_ids = analysis.get_image_ids()

figsize=(20,15)
fig, ax = plt.subplots(7,8,figsize=figsize, sharex=False, sharey=False)
ax = ax.ravel()
i=0
for area in tdf.targeted_structure.unique()[::-1]:
    tmp = tdf[tdf.targeted_structure==area]
    for depth in np.sort(tmp.imaging_depth.unique()):
        tmp2 = tmp[tmp.imaging_depth==depth]
        for experiment_id in tmp2.experiment_id.unique(): 
            edf = tdf[tdf.experiment_id==experiment_id]
            title = edf.targeted_structure.values[0]+', '+str(edf.imaging_depth.values[0])
            ax[i] = plot_mean_image_response_heatmap(edf, image_ids, colorbar=False, title=title, ax=ax[i], save_dir=None)
            ax[i].set_xlabel('')
            ax[i].set_xticklabels('')
            ax[i].set_yticklabels('')
            ax[i].set_ylabel('')
            i+=1
fig.tight_layout()
In [360]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = odf.copy()
df = df[df.oddball==True]
# df = df[df.mean_response>0.05]
# df = df[df.fraction_significant_trials>0.0]

fig, ax = plt.subplots()
ax = sns.pointplot(data=df, x='location', y='mean_response', order=locations, ax=ax)
ax.set_xticklabels(locations, rotation=90)
ax.set_xlabel('')
ax.set_title('response magnitude during sequence block')
Out[360]:
Text(0.5, 1.0, 'response magnitude during sequence block')
In [366]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = tdf.copy()
df = df[df.oddball==True]
# df = df[df.mean_response>0.05]
# df = df[df.fraction_significant_trials>0.0]

fig, ax = plt.subplots()
ax = sns.pointplot(data=df, x='location', y='mean_response', order=locations, ax=ax)
ax.set_xticklabels(locations, rotation=90)
ax.set_xlabel('')
ax.set_title('response magnitude during transition control')
Out[366]:
Text(0.5, 1.0, 'response magnitude during transition control')
In [367]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = odf.copy()
df = df[df.oddball==True]
# df = df[df.mean_response>0.05]
# df = df[df.fraction_significant_trials>0.0]

fig, ax = plt.subplots()
ax = sns.pointplot(data=df, x='location', y='fraction_significant_trials', order=locations, ax=ax)
ax.set_xticklabels(locations, rotation=90)
ax.set_xlabel('')
ax.set_title('reliability during sequence block')
Out[367]:
Text(0.5, 1.0, 'reliability during sequence block')
In [369]:
locations = ['VISp_superficial', 'VISp_deep', 'VISpm_superficial', 'VISpm_deep', 'RSP_superficial', 'RSP_deep']

df = tdf.copy()
df = df[df.oddball==True]
# df = df[df.mean_response>0.05]
# df = df[df.fraction_significant_trials>0.0]

fig, ax = plt.subplots()
ax = sns.pointplot(data=df, x='location', y='fraction_significant_trials', order=locations, ax=ax)
ax.set_xticklabels(locations, rotation=90)
ax.set_xlabel('')
ax.set_title('reliability during transition control')
Out[369]:
Text(0.5, 1.0, 'reliability during transition control')

image responsiveness

In [293]:
def get_session_summary_df(fdf, use_events=False):
    # only consider responses to preferred stimulus
    df = fdf[fdf.pref_stim==True].copy() 
    summary_data = []
    for experiment_id in df.experiment_id.unique():
        expt = df[(df.experiment_id == experiment_id)]
        area = expt.targeted_structure.unique()[0]
        if 'location' in fdf.keys():
            location = expt.location.unique()[0]
        else: 
            lcation = None
        cre_line = expt.cre_line.unique()[0]
        mouse_id = expt.donor_id.unique()[0]
        depth = expt.imaging_depth.unique()[0]
        session_type = expt.session_type.unique()[0]
        image_set = expt.image_set.unique()[0]
        experiment_date = expt.experiment_date.unique()[0]
        n_cells = len(expt.cell_specimen_id.unique())
        summary_data.append(
            [experiment_id, mouse_id, session_type, image_set, area, location, cre_line, experiment_date, depth, n_cells])

    columns = ['experiment_id', 'mouse_id', 'session_type','image_set', 'area', 'location', 'cre_line', 
               'experiment_date', 'depth', 'n_cells']
    session_summary_df = pd.DataFrame(data=summary_data, columns=columns)
    return session_summary_df

def add_metric_to_session_summary_df(metric, data, session_summary_df):
    df = data.copy()
    session_summary_df[metric] = None
    for experiment_id in session_summary_df.experiment_id.unique(): 
        metric_values = df[(df.experiment_id==experiment_id)][metric].values
        session_mean = np.nanmean(metric_values)
        idx = session_summary_df[(session_summary_df.experiment_id==experiment_id)].index[0]
        session_summary_df.loc[idx, metric] = session_mean
    return session_summary_df
In [294]:
odf['image_set'] = 'A'
session_summary_df = get_session_summary_df(odf)
session_summary_df
Out[294]:
experiment_id mouse_id session_type image_set area location cre_line experiment_date depth n_cells
0 775058863 416366 behavior_session_2 A RSP RSP_deep Slc17a7 2018-11-07 350 60
1 775613721 416366 behavior_session_3 A VISpm VISpm_superficial Slc17a7 2018-11-08 175 81
2 776727982 416366 behavior_session_4 A VISpm VISpm_deep Slc17a7 2018-11-09 420 108
3 813071318 432373 behavior_session_2 A VISp VISp_deep Slc17a7 2019-01-24 450 98
4 816795279 432373 behavior_session_4 A VISpm VISpm_deep Slc17a7 2019-01-31 375 77
5 818894752 432373 behavior_session_6 A VISpm VISpm_superficial Slc17a7 2019-02-05 125 93
6 826576489 433242 behavior_session_1 A VISp VISp_superficial Slc17a7 2019-02-20 135 64
7 827232898 433242 behavior_session_2 A VISp VISp_deep Slc17a7 2019-02-21 410 75
8 828956958 433242 behavior_session_4 A VISpm VISpm_superficial Slc17a7 2019-02-25 170 43
9 829411383 433242 behavior_session_5 A VISpm VISpm_deep Slc17a7 2019-02-26 385 72
10 848005700 437812 behavior_session_1 A RSP RSP_deep Rbp4 2019-04-08 375 24
11 848690810 437812 behavior_session_2 A VISp VISp_deep Rbp4 2019-04-09 375 62
12 848006710 437813 behavior_session_1 A RSP RSP_deep Rbp4 2019-04-08 375 17
13 848691390 437813 behavior_session_2 A VISp VISp_deep Rbp4 2019-04-09 375 76
14 832601977 438097 behavior_session_2 A RSP RSP_deep Slc17a7 2019-03-01 375 69
15 832617299 438097 behavior_session_3 A VISpm VISpm_superficial Slc17a7 2019-03-05 137 61
16 833599179 438097 behavior_session_4 A VISpm VISpm_deep Slc17a7 2019-03-06 375 38
17 830688059 438378 behavior_session_2 A VISp VISp_deep Slc17a7 2019-02-28 402 124
18 834260382 438382 behavior_session_2 A RSP RSP_deep Slc17a7 2019-03-07 370 114
19 838330377 438382 behavior_session_4 A VISpm VISpm_deep Slc17a7 2019-03-12 375 106
20 833626456 439885 behavior_session_1 A VISp VISp_superficial Slc17a7 2019-03-06 155 116
21 836248932 439885 behavior_session_2 A VISp VISp_deep Slc17a7 2019-03-12 395 93
22 837630919 439885 behavior_session_3 A VISpm VISpm_superficial Slc17a7 2019-03-13 185 48
23 837287590 439885 behavior_session_4 A VISpm VISpm_deep Slc17a7 2019-03-14 365 32
24 827235482 439895 behavior_session_1 A VISp VISp_superficial Slc17a7 2019-02-21 175 70
25 828959377 439895 behavior_session_2 A VISp VISp_deep Slc17a7 2019-02-25 400 74
26 829417358 439895 behavior_session_3 A VISpm VISpm_superficial Slc17a7 2019-02-26 165 16
27 831314921 439895 behavior_session_4 A VISpm VISpm_deep Slc17a7 2019-03-01 405 65
28 833612445 439900 behavior_session_1 A RSP RSP_superficial Slc17a7 2019-03-06 165 56
29 835660148 439900 behavior_session_3 A VISpm VISpm_superficial Slc17a7 2019-03-11 175 37
30 836253258 439900 behavior_session_4 A VISpm VISpm_deep Slc17a7 2019-03-12 375 36
31 836906598 439900 behavior_session_5 A RSP RSP_deep Slc17a7 2019-03-13 375 20
32 836890936 440299 behavior_session_4 A VISpm VISpm_deep Slc17a7 2019-03-13 375 74
33 837283374 440299 behavior_session_r A VISpm VISpm_superficial Slc17a7 2019-03-14 155 70
In [295]:
df = session_summary_df.copy()
figsize = (5,4)
fig, ax = plt.subplots(figsize=figsize)
ax = sns.stripplot(data=df, x='area', y='n_cells', hue='image_set', ax = ax, dodge=True)
ax.set_ylabel('# cells per session')
fig.tight_layout()
sf.save_figure(fig, figsize, save_dir, 'session_numbers', 'n_cells_by_image_set')
In [296]:
def adjust_box_widths(ax, fac):
    from matplotlib.patches import PathPatch
    # Adjust the withs of a seaborn-generated boxplot.
    for c in ax.get_children():
        if isinstance(c, PathPatch):
            p = c.get_path()
            verts = p.vertices
            verts_sub = verts[:-1]
            xmin = np.min(verts_sub[:, 0])
            xmax = np.max(verts_sub[:, 0])
            xmid = 0.5 * (xmin + xmax)
            xhalf = 0.5 * (xmax - xmin)
            xmin_new = xmid - fac * xhalf
            xmax_new = xmid + fac * xhalf
            verts_sub[verts_sub[:, 0] == xmin, 0] = xmin_new
            verts_sub[verts_sub[:, 0] == xmax, 0] = xmax_new
            for l in ax.lines:
                if np.all(l.get_xdata() == [xmin, xmax]):
                    l.set_xdata([xmin_new, xmax_new])


def plot_boxplot_for_condition(df, metric, condition='image_set', condition_values=['A', 'B', 'C', 'D'],
                               colors=sns.color_palette(), hue='cre_line', ylabel=None, 
                               range=(0, 1), ax=None, save_figures=False, save_dir=None, folder=None):
    if ax is None:
        figsize = (4.5, 4.5)
        fig, ax = plt.subplots(figsize=figsize)
    ax = sns.boxplot(data=df, x=condition, y=metric,
                     hue=hue, ax=ax, width=0.4, dodge=True, palette=colors)
    if ylabel is None:
        ax.set_ylabel('fraction of cells per session')
    else:
        ax.set_ylabel(ylabel)
    ax.set_ylim(range[0] - 0.05, range[1] + .05)
    ax.get_legend().remove()
    ax.set_title(metric)
    sns.despine(offset=10, trim=True)
    plt.gcf().subplots_adjust(top=0.85)
    plt.gcf().subplots_adjust(left=0.25)
    plt.gcf().subplots_adjust(right=0.85)
    plt.gcf().subplots_adjust(bottom=0.25)
    if save_figures:
        psf.save_figure(fig, figsize, save_dir, folder, metric + '_by_' + condition + '_box')
    return ax
In [297]:
def plot_boxplot_and_swarm_for_condition(df, metric, condition='cre_line', condition_values=None,
                                         colors=sns.color_palette(), hue='image_set', hue_order=None, order=None, ylabel=None, xlabel=None, title=None,
                                         plot_swarm=True, show_stats=True, range=(0,1), ax=None, save_figures=False, save_dir=None, folder=None, suffix=''):
    df[metric] = pd.to_numeric(df[metric])
    if hue_order is None: 
        hue_order = np.sort(df[hue].unique())
    if ax is None:
        figsize = (4.5,4.5)
        fig,ax = plt.subplots(figsize=figsize)
    if order is None: 
        order = condition_values
    ax = sns.boxplot(data=df, x=condition, y=metric, hue_order=hue_order, order=order,
                 hue=hue, ax=ax, dodge=True, palette=colors) #color='white',
    adjust_box_widths(ax, 0.8)
    if plot_swarm:
        ax = sns.swarmplot(data=df, x=condition, y=metric, order=order,
                      size=3, ax=ax, hue=hue, hue_order=hue_order, color='.3', dodge=True) #palette=colors,
        swarm_cols = ax.collections
        for swarm in swarm_cols:
            swarm.set_facecolors([0.6,0.6,0.6])

    if ylabel is None:
        ax.set_ylabel('fraction of cells per session')
    else:
        ax.set_ylabel(ylabel)
    if xlabel is None:
        if len(condition.split('_'))>1: 
            ax.set_xlabel(condition.split('_')[0]+' '+condition.split('_')[1])
        else: 
            ax.set_xlabel(condition)
    else:
        ax.set_xlabel(xlabel)
    ax.set_xticklabels(condition_values, rotation=90)
    if title is None:
        ax.set_title(metric)
    else:
        ax.set_title(title)
    ax.set_ylim(range[0]-0.05,range[1]+.05)
    ax.get_legend().remove()
    sns.despine(offset=10, trim=True)
    
    if show_stats: 
        hue_names = hue_order
        sig_y_val = df[metric].max()
        
        stats_df = get_stats_for_conditions(df, metric, condition, condition_values, hue, hue_names)
        label_df = get_label_df(ax, stats_df, condition, condition_values, hue, hue_names)
        ax = label_ax_with_stats(ax, label_df, stats_df, sig_y_val, condition, condition_values, hue, hue_names)
    
    ax.set_ylim(range[0]-0.05,range[1]+.05)
    plt.gcf().subplots_adjust(top=0.85)
    plt.gcf().subplots_adjust(left=0.25)
    plt.gcf().subplots_adjust(right=0.85)
    plt.gcf().subplots_adjust(bottom=0.25)
    if save_figures:
#         l = ax.legend(title=condition, fontsize='small')
#         plt.setp(l.get_title(),fontsize='small')
        sf.save_figure(fig ,figsize, save_dir, folder, metric+'_by_'+condition+'_box_swarm'+suffix)

    return ax
In [298]:
df = odf.copy()
df = df[df.pref_stim==True]
metric = 'fraction_significant_trials'
fraction_responsive_list = []
for experiment_id in session_summary_df.experiment_id.unique():
    expt = df[(df.experiment_id == experiment_id)]
    n_cells = len(expt.cell_specimen_id.unique())
    if np.isnan(expt.fraction_significant_trials.values[0])==False: #if has omitted
        n_responsive = len(expt[expt.fraction_significant_trials>0.25])
    else: 
        n_responsive = np.nan
    fraction = n_responsive / float(n_cells)
    fraction_responsive_list.append(fraction)

session_summary_df['fraction_responsive_'+metric] = fraction_responsive_list
In [405]:
session_summary_df['fraction_responsive_oddball'] = session_summary_df.fraction_responsive_fraction_significant_trials.values
In [406]:
areas = ['VISp', 'VISpm', 'RSP']
In [408]:
metric = 'fraction_responsive_oddball'
xlabel = ''
title = 'fraction responsive cells\nsequence block'
ylabel = 'fraction responsive cells'

hue = 'area'
hue_order = np.sort(session_summary_df.area.unique())
colors = sns.color_palette()

df = session_summary_df.copy()
df[metric] = pd.to_numeric(df[metric])

#     df['cre line'] = [cre_line.split('-')[0] for cre_line in df.cre_line.values]
condition = 'area'
condition_values = np.sort(df['area'].unique())

plot_boxplot_and_swarm_for_condition(df, metric, condition, condition_values, colors, hue, hue_order=hue_order,
                                     plot_swarm=True, ylabel=ylabel, xlabel=xlabel, title=title, range=(0,1), ax=None, 
                                     order=areas, suffix='', save_figures=True, save_dir=save_dir, folder='fraction_responsive', 
                                     show_stats=False)
C:\Anaconda\envs\visual_behavior_sdk\lib\site-packages\ipykernel_launcher.py:18: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
Out[408]:
<matplotlib.axes._subplots.AxesSubplot at 0x1b10a9d0240>
In [409]:
metric = 'fraction_responsive'
xlabel = ''
title = 'fraction responsive cells\nsequences'
ylabel = 'fraction responsive cells'

hue = 'area'
hue_order = np.sort(session_summary_df.area.unique())
colors = sns.color_palette()

df = session_summary_df.copy()
df[metric] = pd.to_numeric(df[metric])

#     df['cre line'] = [cre_line.split('-')[0] for cre_line in df.cre_line.values]
condition = 'area'
condition_values = np.sort(df['area'].unique())
order = areas

fig, ax = plt.subplots(figsize=(4,4))
ax = sns.boxplot(data=df, x=condition, y=metric, hue_order=hue_order, order=order,
                 hue=hue, ax=ax, dodge=False, palette=colors, width=0.3) #color='white',

ax = sns.swarmplot(data=df, x=condition, y=metric, order=order,
                  size=3, ax=ax, hue=hue, hue_order=hue_order, color='.3', dodge=False) #palette=colors,

ax.legend_.remove()
ax.set_ylim(0,1)
ax.set_xlabel('')
ax.set_title(title)
sns.despine()
In [ ]:
 
In [309]:
metric = 'oddball_response_index'
xlabel = ''
title = 'oddball response index'
ylabel = title

hue = 'location'
hue_order = locations
colors = sns.color_palette()
order = locations

df = odf.copy()
df[metric] = pd.to_numeric(df[metric])
df = df[(df[metric]<2)&(df[metric]>-2)]
df = df[df.mean_response>0.05]
                       
#     df['cre line'] = [cre_line.split('-')[0] for cre_line in df.cre_line.values]
condition = 'location'
condition_values = np.sort(df['location'].unique())

fig, ax = plt.subplots()

ax = sns.boxenplot(data=df, y=condition, x=metric, hue_order=hue_order, order=order,
                 hue=hue, ax=ax, dodge=True, palette=colors, orient='h') #color='white',

ax.legend_.remove()
ax.set_ylabel('')
# plot_boxplot_and_swarm_for_condition(df, metric, condition, condition_values, colors, hue, hue_order=hue_order,
#                                      order=None, plot_swarm=False, ylabel=ylabel, xlabel=xlabel, title=title, range=(-2,2), ax=None, 
#                                      suffix='', save_figures=True, save_dir=save_dir, folder='fraction_responsive', 
#                                      show_stats=False)
'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'.  Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.
'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'.  Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.
'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'.  Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.
'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'.  Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.
'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'.  Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.
'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'.  Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.
Out[309]:
Text(0, 0.5, '')

fraction responsive across stimulus types

In [358]:
analysis.response_df_dict.keys()
Out[358]:
dict_keys(['randomized_control_pre', 'oddball', 'transition_control', 'occlusion', 'natural_movie_one', 'randomized_control_post'])
In [371]:
df = tdf.copy()
df = df[df.pref_stim==True]
metric = 'fraction_significant_trials'
fraction_responsive_list = []
for experiment_id in session_summary_df.experiment_id.unique():
    expt = df[(df.experiment_id == experiment_id)]
    n_cells = len(expt.cell_specimen_id.unique())
    if np.isnan(expt.fraction_significant_trials.values[0])==False: #if has omitted
        n_responsive = len(expt[expt.fraction_significant_trials>0.25])
    else: 
        n_responsive = np.nan
    fraction = n_responsive / float(n_cells)
    fraction_responsive_list.append(fraction)

session_summary_df['fraction_responsive_'+metric] = fraction_responsive_list
In [375]:
session_summary_df['fraction_responsive_transition'] = session_summary_df.fraction_responsive_fraction_significant_trials.values
In [376]:
metric = 'fraction_responsive_transition'
xlabel = ''
title = 'fraction responsive cells\ntransition control'
ylabel = 'fraction responsive cells'

hue = 'area'
hue_order = np.sort(session_summary_df.area.unique())
colors = sns.color_palette()

df = session_summary_df.copy()
df[metric] = pd.to_numeric(df[metric])

#     df['cre line'] = [cre_line.split('-')[0] for cre_line in df.cre_line.values]
condition = 'area'
condition_values = np.sort(df['area'].unique())
order = areas

fig, ax = plt.subplots(figsize=(4,4))
ax = sns.boxplot(data=df, x=condition, y=metric, hue_order=hue_order, order=order,
                 hue=hue, ax=ax, dodge=False, palette=colors, width=0.3) #color='white',

ax = sns.swarmplot(data=df, x=condition, y=metric, order=order,
                  size=3, ax=ax, hue=hue, hue_order=hue_order, color='.3', dodge=False) #palette=colors,

ax.legend_.remove()
ax.set_ylim(0,1)
ax.set_xlabel('')
ax.set_title(title)
sns.despine()

images

In [412]:
images
Out[412]:
[68.0,
 78.0,
 13.0,
 26.0,
 6.0,
 17.0,
 22.0,
 51.0,
 71.0,
 89.0,
 103.0,
 110.0,
 111.0,
 112.0]
In [423]:
st.keys()
Out[423]:
Index(['index', 'sweep', 'start_frame', 'end_frame_inclusive', 'start_time',
       'end_time', 'session_block_name', 'image_id', 'repeat',
       'fraction_occlusion', 'duration', 'session_type', 'stimulus_key',
       'data_file_index', 'data_file_name', 'frame_list'],
      dtype='object')

occlusion

In [460]:
np.sort(st[st.session_block_name=='occlusion'].fraction_occlusion.unique())
Out[460]:
array([0.  , 0.15, 0.3 , 0.4 , 0.5 , 0.6 ])
In [458]:
st.keys()
Out[458]:
Index(['index', 'sweep', 'start_frame', 'end_frame_inclusive', 'start_time',
       'end_time', 'session_block_name', 'image_id', 'repeat',
       'fraction_occlusion', 'duration', 'session_type', 'stimulus_key',
       'data_file_index', 'data_file_name', 'frame_list'],
      dtype='object')
In [461]:
st = dataset.stimulus_table
st = st[st.session_block_name=='occlusion']
occlusion_images = st[st.session_block_name=='occlusion'].image_id.unique()
occlusion_fractions = np.sort(st[st.session_block_name=='occlusion'].fraction_occlusion.unique())
image_dict = {}
for image_id in occlusion_images: 
    image_dict[image_id] = {}
    for fraction_occlusion in occlusion_fractions:
        file_name = st[(st.image_id==image_id)&(st.fraction_occlusion==fraction_occlusion)].iloc[0].data_file_name
        file_index = st[(st.image_id==image_id)&(st.fraction_occlusion==fraction_occlusion)].iloc[0].data_file_index
        print(file_name, file_index)
        image_data = np.load(file_name)
        print(image_data.shape)
        image_dict[image_id,fraction_occlusion] = image_data[file_index, :,:]
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 40
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 0
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 10
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 132
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 92
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 74
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 1
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 126
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 14
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 4
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 66
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 53
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 8
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 24
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 25
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 98
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 2
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 41
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 90
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 31
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 20
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 3
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 7
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 27
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 67
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 47
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 50
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 101
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 44
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 6
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 15
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 133
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 9
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 122
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 89
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 32
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 12
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 35
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 29
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 91
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 16
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 17
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 18
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 68
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 39
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 65
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 172
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 294
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 104
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 30
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 42
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 114
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 80
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 19
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 23
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 33
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 55
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 143
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 138
(600, 600, 960)
//allen/aibs/technology/nicholasc/openscope/ophys_pilot_occlusion_659b61b30d270bb849a7c4a903125e02.npy 34
(600, 600, 960)
In [479]:
figsize = (15,15)
fig, ax = plt.subplots(10,6,figsize=figsize, sharex=True, sharey=True)
ax = ax.ravel()
i=0
for y, image_id in enumerate(occlusion_images): 
    for fraction_occlusion in occlusion_fractions:
        ax[i].axes.get_xaxis().set_ticks([])
        ax[i].axes.get_yaxis().set_ticks([])
        ax[i].imshow(image_dict[image_id, fraction_occlusion], cmap='gray')
        if y == 0:
            ax[i].set_title(str(fraction_occlusion))
#         ax[i].axis('off')
        ax[i].set_xticklabels('')
        ax[i].set_yticklabels('')
        if fraction_occlusion == 0: 
            ax[i].set_ylabel(int(image_id))
        i+=1
fig.tight_layout()
In [ ]:
 
In [ ]:
 

oddball

In [428]:
st = dataset.stimulus_table
st = st[st.session_block_name=='oddball']
image_dict = {}
for image_id in images: 
    file_name = st[st.image_id==image_id].iloc[0].data_file_name
    file_index = st[st.image_id==image_id].iloc[0].data_file_index
    print(file_name, file_index)
    image_data = np.load(file_name)
    print(image_data.shape)
    image_dict[image_id] = image_data[file_index, :,:]
//allen/aibs/technology/nicholasc/openscope/68_78_13_26_2950e8d1e5187ce65ac40f5381be0b3f.npy 0
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_26_2950e8d1e5187ce65ac40f5381be0b3f.npy 1
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_26_2950e8d1e5187ce65ac40f5381be0b3f.npy 2
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_26_2950e8d1e5187ce65ac40f5381be0b3f.npy 3
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_6_e7c9e5b69add976b510ee8e85fc9a451.npy 3
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_17_b4466dd964710092bbbce1a5c62acd38.npy 3
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_22_eb69aeef83b4852217df8f8a2eb529c7.npy 3
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_51_b8b6a093ff7955ea8fd2cb48bd5ffa3c.npy 3
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_71_59880b3ff357998b5fac5b79b60c1e92.npy 3
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_89_0ba9d593323f104e447af570d8f1e9bf.npy 3
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_103_03ef13047bedfa9a6981c190ff29eb1a.npy 3
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_110_60f4700eaa8f6cb0ca07027482398e36.npy 3
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_111_a8465d0d71cace488afe32c2dc977eb5.npy 3
(4, 600, 960)
//allen/aibs/technology/nicholasc/openscope/68_78_13_112_8df3919368d45af726c7d3f57f88d9db.npy 3
(4, 600, 960)
In [429]:
image_dict[112].shape
Out[429]:
(600, 960)
In [433]:
plt.imshow(image_dict[112], cmap='gray')
plt.axis('off')
Out[433]:
(-0.5, 959.5, 599.5, -0.5)
In [434]:
figsize = (14,5)
fig, ax = plt.subplots(2,7,figsize=figsize, sharex=True, sharey=True)
ax = ax.ravel()
for i,image_id in enumerate(images): 
    ax[i].imshow(image_dict[image_id], cmap='gray')
    ax[i].set_title(str(int(image_id)))
    ax[i].axis('off')
fig.tight_layout()
In [436]:
analysis.get_sequence_images()[:2]
Out[436]:
[68.0, 78.0]
In [439]:
figsize = (10,20)
fig, ax = plt.subplots(11,4,figsize=figsize, sharex=True, sharey=True)
ax = ax.ravel()
i=0
for x, image_id in enumerate(analysis.get_sequence_images()):
    ax[i].imshow(image_dict[image_id], cmap='gray')
    ax[i].set_title(str(int(image_id)))
    ax[i].axis('off')
    i+=1
for x,oddball_id in enumerate(analysis.get_oddball_images()):
    for x, image_id in enumerate(analysis.get_sequence_images()[:3]):
        ax[i].imshow(image_dict[image_id], cmap='gray')
        ax[i].set_title(str(int(image_id)))
        ax[i].axis('off')
        i+=1
    ax[i].imshow(image_dict[oddball_id], cmap='gray')
    ax[i].set_title(str(int(oddball_id)))
    ax[i].axis('off')
    i+=1
fig.tight_layout()

transition control

In [444]:
analysis.get_sequence_images()[2]
Out[444]:
13.0
In [446]:
analysis.get_sequence_images()[:1]
Out[446]:
[68.0]
In [450]:
figsize = (5,40)
fig, ax = plt.subplots(24,2,figsize=figsize, sharex=True, sharey=True)
ax = ax.ravel()
i=0
for x, image_id in enumerate(analysis.get_sequence_images()[:2]):
    ax[i].imshow(image_dict[image_id], cmap='gray')
    ax[i].set_title(str(int(image_id)))
    ax[i].axis('off')
    i+=1
for x, image_id in enumerate(analysis.get_sequence_images()[1:3]):
    ax[i].imshow(image_dict[image_id], cmap='gray')
    ax[i].set_title(str(int(image_id)))
    ax[i].axis('off')
    i+=1
for x, image_id in enumerate(analysis.get_sequence_images()[2:]):
    ax[i].imshow(image_dict[image_id], cmap='gray')
    ax[i].set_title(str(int(image_id)))
    ax[i].axis('off')
    i+=1
for x,oddball_id in enumerate(analysis.get_oddball_images()):
    image_id = analysis.get_sequence_images()[2]
    ax[i].imshow(image_dict[image_id], cmap='gray')
    ax[i].set_title(str(int(image_id)))
    ax[i].axis('off')
    i+=1
    ax[i].imshow(image_dict[oddball_id], cmap='gray')
    ax[i].set_title(str(int(oddball_id)))
    ax[i].axis('off')
    i+=1
for x,oddball_id in enumerate(analysis.get_oddball_images()):
    ax[i].imshow(image_dict[oddball_id], cmap='gray')
    ax[i].set_title(str(int(oddball_id)))
    ax[i].axis('off')
    i+=1
    image_id = analysis.get_sequence_images()[0]
    ax[i].imshow(image_dict[image_id], cmap='gray')
    ax[i].set_title(str(int(image_id)))
    ax[i].axis('off')
    i+=1
fig.tight_layout()
In [ ]: